问题描述
我希望使用log4j2从我的Web应用程序和服务器(tomcat 6)本身进行日志记录,最好是全部记录到同一文件.我拥有的配置适用于Web应用程序,但不适用于服务器类.
I am hoping to use log4j2 to log from both my web applications and the server (tomcat 6) itself, ideally all to the same file. The config I have works fine for the web applications, but not for the server classes.
我想要的是将类似以下内容的行写入文件(它们当前仅写入控制台)
What I want is for lines like the following to be written to a file, (they are currently written to the console only)
Aug 15, 2014 1:03:24 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-80
Aug 15, 2014 1:03:24 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1287 ms
我在tomcat/lib目录中有log4j-api-2.0.1.jar,log4j-core-2.0.1.jar和log4j2.xml.下面显示了一个简化的log4j2.xml.我没有看到status ="all"给出的信息有任何问题. log4j2会自动进行自我设置-我没有使用CATALINA_OPTS传递configurationFile.
I have log4j-api-2.0.1.jar, log4j-core-2.0.1.jar and log4j2.xml in the tomcat/lib directory. A stripped down log4j2.xml is shown below. I don't see any problems in the information given by having status="all". log4j2 is setting itself up automatically - I am not passing in a configurationFile with CATALINA_OPTS.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="all">
<Appenders>
<RollingFile name="R" fileName="../logs/general.log" filePattern="../logs/general-%d{yyyy-MM-dd}.log" append="true">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="1000"/>
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="R" level="info"/>
<AppenderRef ref="STDOUT" level="error"/>
</Root>
</Loggers>
</Configuration>
知道我在做什么错吗?
推荐答案
在回答这个问题并再次访问它之间,我升级到了tomcat8.这就是我设法使其工作的方式.此方法需要log4j2的版本2.1,并且在某种程度上假定您已经使其适用于Web应用程序.说明假定CATALINA_HOME和CATALINA基数不是分开的
In between answering this question and revisiting it I upgraded to tomcat 8. This is how I managed to get it working. This method requires version 2.1 of log4j2, and somewhat assumes you have already got it working for your web application. Instructions assume CATALINA_HOME and CATALINA base are not separate
基于 http://tomcat.apache.org/tomcat- 8.0-doc/logging.html#Using_Log4j
- 将log4j2.xml放入$ CATALINA_BASE/lib中.您可能希望为org.apache.catalina.core.ContainerBase.[Catalina].[localhost],org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]具有单独的附加程序和org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]
- 从其他工具中下载tomcat-juli.jar和tomcat-juli-adapters.jar(通过 http: //tomcat.apache.org/download-80.cgi )
- 将log4j-api-2.1.jar,log4j-core-2.1.jar,log4j-jul-2.1.jar和tomcat-juli-adapters.jar从"extras"放入$ CATALINA_HOME/lib.
- 用"extras"中的tomcat-juli.jar替换$ CATALINA_HOME/bin/tomcat-juli.jar.
- 删除$ CATALINA_BASE/conf/logging.properties
- Put log4j2.xml in $CATALINA_BASE/lib . You may wish to have separate appenders for org.apache.catalina.core.ContainerBase.[Catalina].[localhost], org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]and org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]
- Download tomcat-juli.jar and tomcat-juli-adapters.jar from extras (via http://tomcat.apache.org/download-80.cgi )
- Put log4j-api-2.1.jar, log4j-core-2.1.jar, log4j-jul-2.1.jar, and tomcat-juli-adapters.jar from "extras" into $CATALINA_HOME/lib.
- Replace $CATALINA_HOME/bin/tomcat-juli.jar with tomcat-juli.jar from "extras".
- Delete $CATALINA_BASE/conf/logging.properties
我添加的额外步骤是将日志记录管理器设置为使用log4j2-jul桥(log4j-jul-2.1.jar)中的管理器.更改catalina.bat或catalina.sh以确保类路径包括bin/tomcat-juli.jar,lib/log4j-jul-2.1.jar,lib/log4j-api-2.1.jar和lib/log4j-core-2.1. jar,并且用于启动tomcat的命令包括
The extra step I added was to set the logging manager to use the manager from the log4j2-jul bridge (log4j-jul-2.1.jar). Alter catalina.bat or catalina.sh to ensure that the classpath includes bin/tomcat-juli.jar, lib/log4j-jul-2.1.jar, lib/log4j-api-2.1.jar and lib/log4j-core-2.1.jar, and the command used to start tomcat includes
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
这篇关于使用log4j2在Tomcat 6中记录服务器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!