我的 log4j 配置有问题。
在 Maven 将我的应用程序打包到 war-file 之前,它会运行测试。并且加载和使用 log4j 配置 - 创建日志文件并在其中和控制台中写入消息。
控制台中有 log4j 调试输出:
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@422ede.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@422ede class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@422ede.
log4j: Using URL [file:/C:/dev/workspace/paymentsystemsstub/target/classes/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/C:/dev/workspace/paymentsystemsstub/target/classes/log4j.properties
log4j: Parsing for [root] with value=[WARN, file, stdout].
log4j: Level token is [WARN].
log4j: Category root set to WARN
log4j: Parsing appender named "file".
log4j: Parsing layout options for "file".
log4j: Setting property [conversionPattern] to [%d{dd MMM yyyy HH:mm:ss,SSS} [%c] [%-5p] %n%m%n].
log4j: End of parsing for "file".
log4j: Setting property [file] to [C:\tomcat\log/paymentSystemsStub.log].
log4j: Setting property [maxBackupIndex] to [1].
log4j: Setting property [maxFileSize] to [1MB].
log4j: setFile called: C:\tomcat\log/paymentSystemsStub.log, true
log4j: setFile ended
log4j: Parsed "file" options.
log4j: Parsing appender named "stdout".
log4j: Parsing layout options for "stdout".
log4j: Setting property [conversionPattern] to [%d{HH:mm:ss,SSS} [%c] [%-5p] %n%m%n].
log4j: End of parsing for "stdout".
log4j: Parsed "stdout" options.
log4j: Parsing for [com.panbet.paymentstub] with value=[DEBUG].
log4j: Level token is [DEBUG].
log4j: Category com.panbet.paymentstub set to DEBUG
log4j: Handling log4j.additivity.com.panbet.paymentstub=[null]
log4j: Finished configuring.
但是当我在 Tomcat 中部署生成的战争文件时,我看到加载了 log4j 配置并创建了日志文件,但消息没有写入其中或控制台中:
有 log4j 调试输出:
log4j: Trying to find [log4j.xml] using context classloader WebappClassLoader
context: /paymentstub
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@d1ad08
.
log4j: Trying to find [log4j.xml] using WebappClassLoader
context: /paymentstub
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@d1ad08
class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader WebappClassLoader
context: /paymentstub
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@d1ad08
.
log4j: Using URL [file:/C:/tomcat/webapps/paymentstub/WEB-INF/classes/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/C:/tomcat/webapps/paymentstub/WEB-INF/classes/log4j.properties
log4j: Parsing for [root] with value=[WARN, file, stdout].
log4j: Level token is [WARN].
log4j: Category root set to WARN
log4j: Parsing appender named "file".
log4j: Parsing layout options for "file".
log4j: Setting property [conversionPattern] to [%d{dd MMM yyyy HH:mm:ss,SSS} [%c] [%-5p] %n%m%n].
log4j: End of parsing for "file".
log4j: Setting property [file] to [C:\tomcat\log/paymentSystemsStub.log].
log4j: Setting property [maxBackupIndex] to [1].
log4j: Setting property [maxFileSize] to [1MB].
log4j: setFile called: C:\tomcat\log/paymentSystemsStub.log, true
log4j: setFile ended
log4j: Parsed "file" options.
log4j: Parsing appender named "stdout".
log4j: Parsing layout options for "stdout".
log4j: Setting property [conversionPattern] to [%d{HH:mm:ss,SSS} [%c] [%-5p] %n%m%n].
log4j: End of parsing for "stdout".
log4j: Parsed "stdout" options.
log4j: Parsing for [com.panbet.paymentstub] with value=[DEBUG].
log4j: Level token is [DEBUG].
log4j: Category com.panbet.paymentstub set to DEBUG
log4j: Handling log4j.additivity.com.panbet.paymentstub=[null]
log4j: Finished configuring.
文件log4j.properties
夹WEB-INF\classes
和文件夹log4j-1.2.17.jar
中有文件WEB-INF\lib
(从这里和这里它们应该在那里)。
我传递了一个绝对路径来创建日志文件作为 JVM 属性。
有我的log4j.properties
文件:
# Set root logger level to WARN and its appenders to file and stdout
log4j.rootLogger=WARN, file, stdout
# stdout is set to be a ConsoleAppender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# stdout uses PatternLayout
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%c] [%-5p] %n%m%n
# file is set to be a RollingFileAppender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${stubLog}/paymentSystemsStub.log
log4j.appender.file.MaxFileSize=1MB
# Keep one backup file
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} [%c] [%-5p] %n%m%n
# Print only messages of level DEBUG or above in the package com.panbet.paymentstub
log4j.logger.com.panbet.paymentstub=DEBUG
我正在使用 Windows 7、Eclipse、Tomcat 7、Maven 和 Log4j 1.2。
我整天都在试图找出问题所在,但仍然不知道。
更新
我感到很困惑!结果,我调用的方法没有记录任何东西!我很确定他们会...抱歉打扰。