下面描述的几个简单步骤将帮助您设置要初始化的 Web 应用程序的 log4j:
1)复制log4j.xml
到您的(TOMCAT_HOME)/CATALINA_HOME/conf
目录。我的log4j.xml
应用程序如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE}
%5p %c{1}:%L -Preetam %m%n"/>
</layout>
</appender>
<root>
<!-- <priority value="info"></priority> -->
<priority value="debug"></priority>
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
2)停止tomcat
tomcat6 stop /var/tmp/tomcat-20120622.log (use your application specific stop.sh script)
3)在(TOMCAT_HOME)/CATALINA_HOME/conf
目录中,您将拥有该tomcat6.conf
文件。如下所述编辑文件。如果 中没有CATALINA_OPTS
,则tomcat6.conf
创建一个。
# Settings for the CATALINA_OPTS to pick web application's log4j.xml from the specified location
CATALINA_OPTS="${CATALINA_OPTS}-Dlog4j.debug - Dlog4j.configuration=file:${CATALINA_HOME}/conf/log4j.xml"
4) 使用您的应用程序特定startup.sh
脚本启动 tomcat。
5) 就这样,你们都完成了。查看日志看tomcat是否在读取log4j.xml
你指定的tomcat6.conf
.
tail -f /var/tmp/tomcat-20120622.log
Jun 22, 2012 3:28:35 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Jun 22, 2012 3:28:35 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jun 22, 2012 3:28:35 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jun 22, 2012 3:28:35 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/12 config=null
Jun 22, 2012 3:28:35 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 547 ms
log4j: Using URL [file:/opt/otccalypso/guiadmd01/tomcat6/conf/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: Threshold ="null".
log4j: Level value for root is [debug].
log4j: root level set to DEBUG
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{ABSOLUTE} %5p %c{1}:%L -Preetam %m%n].
log4j: Adding appender named [stdout] to category [root].