[英]Converting log4j.properties file for Tomcat to work with Log4j2
I'm trying to configure Tomcat 8 to use Log4j2 for logging. 我正在尝试配置Tomcat 8以使用Log4j2进行日志记录。
I've found this reference for Logging in Tomcat using Log4j . 我已经找到了使用Log4j登录Tomcat的这个参考。 It provides a sample log4j.properties file that configures Log4j to match Tomcat's internal logging. 它提供了一个示例log4j.properties文件,该文件配置Log4j以匹配Tomcat的内部日志记录。 Most of this looks pretty straightforward to convert for Log4j2, but the section at the end that maps loggers to appenders has me stumped: 转换为Log4j2的大部分内容看起来非常简单,但最后将记录器映射到appender的部分让我感到难过:
# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] =\
INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] =\
INFO, HOST-MANAGER
Has anyone converted this configuration to work with Log4j2? 有没有人将此配置转换为与Log4j2一起使用? I've been working from the Log4j2 configuration documentation and have read through the Log4j2 Architecture page , but I haven't found much material on how to do this sort of container mapping in Log4j2. 我一直在使用Log4j2配置文档并阅读了Log4j2 Architecture页面 ,但是我没有找到很多关于如何在Log4j2中进行这种容器映射的材料。
I suppose I could do a separate configuration for each container, but I'd prefer to keep it in one place, as in the sample Log4j configuration. 我想我可以为每个容器做一个单独的配置,但我更喜欢将它保存在一个地方,就像示例Log4j配置一样。
After asking this question, I spent some more time with setting up log4j2, and this is the log4j2.xml
file I came up with. 在提出这个问题后,我花了一些时间来设置log4j2,这是我提出的log4j2.xml
文件。 It mimics the configuration described in Logging in Tomcat using Log4j . 它模仿使用Log4j登录Tomcat中描述的配置。 It uses multiple loggers to route messages to separate log files. 它使用多个记录器将消息路由到单独的日志文件。
<?xml version="1.0" encoding="utf-8"?>
<Configuration status="info">
<Properties>
<Property name="logdir">${sys:catalina.base}/logs</Property>
<Property name="layout">%d [%t] %-5p %c- %m%n</Property>
</Properties>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="${layout}"/>
</Console>
<RollingFile name="CATALINA"
fileName="${logdir}/catalina.log"
filePattern="${logdir}/catalina.%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${layout}"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="LOCALHOST"
fileName="${logdir}/localhost.log"
filePattern="${logdir}/localhost.%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${layout}"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="MANAGER"
fileName="${logdir}/manager.log"
filePattern="${logdir}/manager.%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${layout}"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="HOST-MANAGER"
fileName="${logdir}/host-manager.log"
filePattern="${logdir}/host-manager.%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${layout}"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="CATALINA"/>
</Root>
<Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]"
level="info" additivity="false">
<AppenderRef ref="LOCALHOST"/>
</Logger>
<Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]"
level="info" additivity="false">
<AppenderRef ref="MANAGER"/>
</Logger>
<Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]"
level="info" additivity="false">
<AppenderRef ref="HOST-MANAGER"/>
</Logger>
</Loggers>
</Configuration>
However, getting this to work properly required getting a bunch of other files configured correctly. 但是,要使其正常工作,需要正确配置一堆其他文件。 The tomcat 7 internal logging with log4j2.xml posting from Paramita Banerjee was helpful with this. 使用来自Paramita Banerjee的log4j2.xml发布的tomcat 7内部日志记录对此很有帮助。
This file goes in CATALINA_HOME/bin/
: 该文件位于CATALINA_HOME/bin/
:
tomcat-juli.jar
If you're pulling this from the Maven repository, you'll get a file named something like tomcat-extras-juli-8.0.15.jar
(the current version when I wrote this). 如果您从Maven存储库中提取此文件,您将获得一个名为tomcat-extras-juli-8.0.15.jar
(当我编写此文件时的当前版本)。 However, it needs to be renamed to tomcat-juli.jar
– the Tomcat setup scripts use that name in setting up the CLASSPATH. 但是,需要将其重命名为tomcat-juli.jar
- Tomcat安装脚本在设置CLASSPATH时使用该名称。
These files go in CATALINA_HOME/lib/
: 这些文件放在CATALINA_HOME/lib/
:
commons-logging-1.2.jar
log4j-1.2-api-2.1.jar
log4j-api-2.1.jar
log4j-core-2.1.jar
log4j-jcl-2.1.jar
log4j-jul-2.1.jar
log4j-web-2.1.jar
tomcat-juli-adapters-8.0.15.jar
log4j-web-2.1.jar
may not be needed here (it may just need to be deployed with your web application) – its use is described in Using Log4j 2 in Web Applications . 这里可能不需要log4j-web-2.1.jar
(它可能只需要与您的Web应用程序一起部署) - 在Web应用程序中使用Log4j 2中描述了它的用法。 log4j-1.2-api-2.1.jar
is needed only if you have applications that use the older log4j 1.2 interface. 仅当您的应用程序使用较旧的log4j 1.2接口时,才需要log4j-1.2-api-2.1.jar
。
In CATALINA_BASE/conf
, I disabled logging.properties
. 在CATALINA_BASE/conf
,我禁用了logging.properties
。
I used the following setenv.sh
script to define the CLASSPATH
and LOGGING_MANAGER
environment variables correctly for Tomcat. 我用下面的setenv.sh
脚本来定义CLASSPATH
和LOGGING_MANAGER
正确的环境变量为Tomcat。 It goes in either CATALINA_BASE/bin
or into CATALINA_HOME/bin
. 它位于CATALINA_BASE/bin
或CATALINA_HOME/bin
。 (I put it in CATALINA_HOME
/bin.) It is executed by Tomcat's startup scripts if it's present. (我把它放在CATALINA_HOME
/ bin中。)它由Tomcat的启动脚本执行,如果它存在的话。 You might prefer something simpler, but this is in keeping with the style of the startup scripts. 您可能更喜欢更简单的东西,但这与启动脚本的风格保持一致。
LOG4J_JARS="log4j-core-2.1.jar log4j-api-2.1.jar log4j-jul-2.1.jar"
# make log4j2.xml available
if [ ! -z "$CLASSPATH" ] ; then CLASSPATH="$CLASSPATH": ; fi
CLASSPATH="$CLASSPATH""$CATALINA_BASE"/lib
# Add log4j2 jar files to CLASSPATH
for jar in $LOG4J_JARS ; do
if [ -r "$CATALINA_HOME"/lib/"$jar" ] ; then
CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/lib/"$jar"
else
echo "Cannot find $CATALINA_HOME/lib/$jar"
echo "This file is needed to properly configure log4j2 for this program"
exit 1
fi
done
# use the logging manager from log4j-jul
LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"
As Nick mentioned in his response, there's also the output to the access log. 正如尼克在回答中提到的那样,访问日志也有输出。 I haven't tried to do anything with that, either. 我也没有试图做任何事情。
I hope others find this useful. 我希望其他人觉得这很有用。 In retrospect, it seems pretty straightforward. 回想起来,它看起来非常简单。 However, there are a lot of parts that have to be “just right” for it to work, and that was a challenge (at least for a newbie). 然而,有许多部分必须“恰到好处”才能发挥作用,这是一个挑战(至少对于新手而言)。
I followed the reference for Logging in Tomcat using Log4j and added tomcat-juli.jar to the bin dir and I also added tomcat-juli-adapters.jar to the lib dir. 我使用Log4j跟踪了Logging in Tomcat的引用,并将tomcat-juli.jar添加到bin目录中,我还将tomcat-juli-adapters.jar添加到lib目录中。 Afterwards i added the log4j-api-2.1.jar, log4j-core-2.1.jar and log4j-1.2-api-2.1.jar to the lib dir. 然后我将log4j-api-2.1.jar,log4j-core-2.1.jar和log4j-1.2-api-2.1.jar添加到lib目录中。
After that I added log4j2.xml to the lib dir. 之后我将log4j2.xml添加到lib目录中。 I kept the configuration fairly simple using a time and size based rolling configuration that zips the archived logs: 我使用基于时间和大小的滚动配置来保持配置相当简单,该滚动配置会压缩存档日志:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="catalina" packages="">
<Appenders>
<RollingRandomAccessFile name="catalina"
fileName="${sys:catalina.base}/logs/catalina.log"
filePattern="${sys:catalina.base}/logs/catalina/$${date:yyyy-MM}/catalina-%d{yyyy-MM-dd}-%i.log.zip">
<PatternLayout>
<Pattern>%d{MMM d, yyyy HH:mm:ss}: %5p (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
<DefaultRolloverStrategy max="100" />
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<!-- default loglevel for emaxx code -->
<logger name="org.apache.catalina" level="info">
<appender-ref ref="catalina" />
</logger>
<Root level="info">
<appender-ref ref="catalina" />
</Root>
</Loggers>
</Configuration>
This way you'll get all logging in the catalina.log. 通过这种方式,您将获得catalina.log中的所有日志记录。 I'm still working on getting the accessLog to do the same. 我还在努力让accessLog做同样的事情。
EDIT: I found this site. 编辑:我找到了这个网站。 this way you can direct access logging to the catalina.log. 这样您就可以将访问日志记录到catalina.log。 (I couldn't get it to log to it's own appender) (我无法让它登录到它自己的appender)
I didn't worry about the logging for manager and host manager, since we don't have them in production environments, but they might just log to the catalina.log too. 我并不担心经理和主机经理的日志记录,因为我们在生产环境中没有它们,但他们也可能只登录到catalina.log。 I haven't tested it. 我没有测试过。
This was tested on tomcat-7.0.42, it should work in tomcat8 too. 这是在tomcat-7.0.42上测试的,它也应该在tomcat8中工作。
For system properties you need to prefix variables with "sys:" it indicates System Properties 对于系统属性,您需要使用“sys:”为变量添加前缀,它表示系统属性
Example: 例:
appender.file.fileName=${sys:catalina.base}/logs/XXX.log appender.file.fileName = $ {SYS:catalina.base} /logs/XXX.log
for more information go through below link http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution 欲了解更多信息,请访问以下链接http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.