问题描述
我正在尝试配置tomcat 8以使用log4j2进行记录.
我已经找到了此参考,用于使用log4j .它提供了一个示例log4j.properties文件,该文件配置log4j以匹配Tomcat的内部日志记录.其中大多数看起来很简单,可以转换为log4j2,但是最终将记录器映射到附录的部分使我感到困惑:
# 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
有人是否将这种配置转换为与Log4J2一起使用?我一直从log4j2配置文档工作,并且已经通过 log4j2体系结构页面,但是我没有找到有关如何在log4j2中进行这种容器映射的材料.
我想我可以为每个容器做一个单独的配置,但是我宁愿将其保存在一个位置,例如示例log4j配置.
推荐答案
问了这个问题后,我花了更多时间来设置log4j2,这是 log4j2.xml我想出的文件.它模仿配置 在使用log4j 中记录tomcat中.它使用多个记录器路由消息 分开日志文件.
<?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>
但是,让它正常工作需要 其他文件 正确配置. tomcat 7 tomcat 7 Internallogg log4j2.xml 来自Paramita Banerjee的发布对此有所帮助.
此文件在CATALINA_HOME/bin/中:
- tomcat-juli.jar
如果您要从Maven存储库中提取此文件,则会得到一个文件 命名tomcat-extras-juli-8.0.15.jar(当前) 我写这篇文章时).但是,它需要重命名为 tomcat-juli.jar - Tomcat设置脚本在 设置classPath.
这些文件进入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这里可能不需要此处(可能只需要与您的Web应用程序部署) - 它的使用在 在Web Applications中使用log4j 2 . log4j-1.2-api-2.1.jar仅在您有使用的应用程序时才需要 旧的log4j 1.2接口.
在CATALINA_BASE/conf中,我禁用logging.properties.
我使用以下setenv.sh脚本来定义CLASSPATH和 LOGGING_MANAGER tomcat正确的环境变量.它都进入 CATALINA_BASE/bin或进入CATALINA_HOME/bin. (我把它放在 CATALINA_HOME/bin.)如果是,则由Tomcat的启动脚本执行 展示.您可能更喜欢一些简单的东西,但这与 启动脚本的样式.
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"
正如尼克在他的回应中提到的那样,也有输出 访问日志.我也没有尝试过任何事情.
我希望其他人发现这有用.回想起来,看起来很漂亮 直截了当.但是,有很多部分必须 "恰到好处"的工作,这是一个挑战(至少对于一个 newbie).
其他推荐答案
我遵循使用tomcat在Tomcat中使用Log4J登录, 并将tomcat-juli.jar添加到bin dir,我还添加了tomcat-juli-adapters.jar. 之后,我添加了log4j-api-2.1.jar,log4j-core-2.1.jar和log4j-1.2-api-2.1.jar
之后,我将log4j2.xml添加到了lib dir.我使用基于时间和大小的滚动配置来保持配置相当简单,该配置将存档日志汇总:
<?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>
这样,您就可以在catalina.log中获取所有登录.
我仍在努力让AccessLog执行相同的操作.
编辑:我找到了 this 网站.这样,您可以将访问记录定向到catalina.log. (我无法将其登录到自己的appender)
我不必担心经理和主机经理的登录,因为我们在生产环境中没有它们,但是它们也可能只是登录到catalina.log.我没有测试过.
这是在Tomcat-7.0.42上测试的,它也应该在tomcat8中使用.
其他推荐答案
对于系统属性,您需要将变量以" sys:"为前缀变量,它指示系统属性
示例:
appender.file.filename = $ {sys:catalina.base}/logs/xxx.log
有关更多信息,请通过以下链接 http://logging.apache.opache.org/logg/log4j/2. X/Manual/configuration.html#PropertySubstitution
问题描述
I'm trying to configure Tomcat 8 to use Log4j2 for logging.
I've found this reference for Logging in Tomcat using Log4j. It provides a sample log4j.properties file that configures Log4j to match Tomcat's internal logging. Most of this looks pretty straightforward to convert for Log4j2, but the section at the end that maps loggers to appenders has me stumped:
# 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? 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.
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.
推荐答案
After asking this question, I spent some more time with setting up log4j2, and this is the log4j2.xml file I came up with. It mimics the configuration described in Logging in Tomcat using Log4j. 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.
This file goes in 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). However, it needs to be renamed to tomcat-juli.jar – the Tomcat setup scripts use that name in setting up the CLASSPATH.
These files go in 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-1.2-api-2.1.jar is needed only if you have applications that use the older log4j 1.2 interface.
In CATALINA_BASE/conf, I disabled logging.properties.
I used the following setenv.sh script to define the CLASSPATH and LOGGING_MANAGER environment variables correctly for Tomcat. It goes in either CATALINA_BASE/bin or into CATALINA_HOME/bin. (I put it in CATALINA_HOME/bin.) It is executed by Tomcat's startup scripts if it's present. 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. 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.
After that I added log4j2.xml to the lib dir. 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.
I'm still working on getting the accessLog to do the same.
EDIT: I found this site. this way you can direct access logging to the catalina.log. (I couldn't get it to log to it's own 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. I haven't tested it.
This was tested on tomcat-7.0.42, it should work in tomcat8 too.
其他推荐答案
For system properties you need to prefix variables with "sys:" it indicates System Properties
Example:
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