Java Log4j 配置在 Tomcat 上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19981503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log4j configuration does not work on Tomcat
提问by Julia
I have a problem with log4j configuration.
我的 log4j 配置有问题。
Before Maven packages my application in war-file, it runs tests. And log4j configuration is loaded and used - log file is created and messages are written in it and in the console.
在 Maven 将我的应用程序打包到 war 文件中之前,它会运行测试。然后加载并使用 log4j 配置 - 创建日志文件并在其中和控制台中写入消息。
There is log4j debug output in console:
控制台中有 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.
But when I deploy generated war-file in Tomcat, I see that log4j configuration is loaded and the log file is created, but messages are not written in it or in console:
但是当我在 Tomcat 中部署生成的战争文件时,我看到 log4j 配置已加载并创建了日志文件,但消息未写入其中或控制台中:
There is log4j debug output:
有 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.
There are files log4j.properties
in folder WEB-INF\classes
and log4j-1.2.17.jar
in folder WEB-INF\lib
(from hereand herethey should be there).
文件log4j.properties
夹WEB-INF\classes
和文件夹log4j-1.2.17.jar
中有文件WEB-INF\lib
(从这里和这里它们应该在那里)。
I pass an absolute path where to create log file as a JVM property.
我传递了一个绝对路径来创建日志文件作为 JVM 属性。
There is my log4j.properties
file:
有我的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
I am using Windows 7, Eclipse, Tomcat 7, Maven and Log4j 1.2.
我使用的是 Windows 7、Eclipse、Tomcat 7、Maven 和 Log4j 1.2。
I spend all day for trying to find out what's wrong and still have no idea.
我花了一整天的时间试图找出问题所在,但仍然不知道。
Update
更新
I am so confused! Turn out, that methods I invoked do not log anything! I was so sure that they do... Sorry for bother.
我感到很困惑!结果,我调用的方法没有记录任何内容!我很确定他们这样做了……抱歉打扰了。
回答by archetype
@Julia Have you check your $CATALINA_BASE/conf/logging.properties file? You may need to delete it from your TOMCAT folder.
@Julia 你检查过你的 $CATALINA_BASE/conf/logging.properties 文件吗?您可能需要从 TOMCAT 文件夹中删除它。
Follow-up for my answer:
跟进我的回答:
I think your log4j.properties configuration is correct. The next step would be by checking what is the current location of the log configuration that is being loaded. Add -Dlog4j.debug
to your JAVA_OPTS
in TOMCAT
. On this way you can check how the log4j is instantiated and it will show what log4j properties has been loaded. Then try to check information from the tomcat log files catalina.out
or catalina.2013-11-06.log
我认为您的 log4j.properties 配置是正确的。下一步是检查正在加载的日志配置的当前位置。添加-Dlog4j.debug
到您JAVA_OPTS
的TOMCAT
. 通过这种方式,您可以检查 log4j 是如何实例化的,它会显示已加载的 log4j 属性。然后尝试从 tomcat 日志文件中检查信息catalina.out
或catalina.2013-11-06.log
回答by Abderrazak BOUADMA
try to add this to your tomcat launcher in eclipse as JVM params
尝试将此作为 JVM 参数添加到 Eclipse 中的 tomcat 启动器
-Dlog4j.rootLevel="ERROR" -Dlog4j.rootAppender="console"
回答by phamthaithinh
Steps to make Tomcat work with log4j
使 Tomcat 与 log4j 一起工作的步骤
We download two files from extras on tomcat download section
tomcat-juli.jar tomcat-juli-adapters.jar
- Copy tomcat-juli.jar to $CATALINA_HOME\bin to replace old one
- Copy tomcat-juli-adapters.jar to $CATALINA_HOME\lib
- Download log4j lib and copy it to $CATALINA_HOME\libs.
- Delete logging.properties in $CATALINA_HOME\conf
- Create log4j.properties in $CATALINA_HOME\libs.
- Download log4j version greater than 1.2 and copy to $CATALINA_HOME\lib
我们从 tomcat 下载部分的 extras 下载两个文件
tomcat-juli.jar tomcat-juli-adapters.jar
- 将 tomcat-juli.jar 复制到 $CATALINA_HOME\bin 以替换旧的
- 将 tomcat-juli-adapters.jar 复制到 $CATALINA_HOME\lib
- 下载 log4j lib 并将其复制到 $CATALINA_HOME\libs。
- 删除 $CATALINA_HOME\conf 中的 logging.properties
- 在 $CATALINA_HOME\libs 中创建 log4j.properties。
- 下载1.2以上的log4j版本,复制到$CATALINA_HOME\lib
After that restart tomcat.
之后重启tomcat。