问题
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
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>
Any idea what I am doing wrong?
回答1:
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
Based on http://tomcat.apache.org/tomcat-8.0-doc/logging.html#Using_Log4j
- 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
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
回答2:
This is probably possible but may not be easy. Here is a good starting point: http://tomcat.apache.org/tomcat-6.0-doc/logging.html
来源:https://stackoverflow.com/questions/25326414/logging-server-classes-in-tomcat-6-with-log4j2