Log4j配置在Tomcat上不起作用

时间:2013-11-14 15:22:12

标签: java eclipse maven tomcat logging

我遇到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中部署生成的war文件时,我看到加载了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(来自herehere应该在那里)。

我传递一个绝对路径,在那里创建日志文件作为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。

我花了一整天时间试图找出错误但仍然不知道。

更新

我很困惑!事实证明,我调用的方法不记录任何东西!我非常肯定他们这样做...抱歉打扰了。

3 个答案:

答案 0 :(得分:2)

使Tomcat与log4j一起工作的步骤

  • 我们从tomcat下载部分的附加内容下载两个文件

    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中

之后重启tomcat。

答案 1 :(得分:-1)

@Julia您是否检查过您的$ CATALINA_BASE / conf / logging.properties文件?您可能需要将其从TOMCAT文件夹中删除。

跟进我的回答:

我认为你的log4j.properties配置是正确的。下一步是检查正在加载的日志配置的当前位置。将-Dlog4j.debug添加到JAVA_OPTS中的TOMCAT。通过这种方式,您可以检查log4j的实例化方式,并显示已加载的log4j属性。然后尝试检查tomcat日志文件catalina.outcatalina.2013-11-06.log

中的信息

答案 2 :(得分:-1)

尝试将此作为JVM参数添加到eclipse中的tomcat启动器

-Dlog4j.rootLevel="ERROR" -Dlog4j.rootAppender="console"