1.先把把commons-logging.jar 和 log4j-xxx.jar 拷贝到 tomcat.home/common/lib/ 下.在 tomcat.home/common/classes/ 下建个 log4j.properties 就行了.这样所有输出信息可以由 log4j.properties 控制.
log4j.properties 文件:
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=/home/camry/work/logs/xxxx.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
2.若你的 tomcat 中有好几个项目, 想不同项目日志写到不同位置去.可以在项目中创建个 Log4jInitServlet.class 来载入该项目的 log4j.properties.这时可以把 log4j.properties 放到 WEB-INF/ 下, 当然愿意放其他位置也可以.
代码如下:
WEB-INF/web.xml:
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>
com.xxx.xxxx.Log4jInitServlet
</servlet-class>
<init-param>
<param-name>log4j</param-name>
<!-- 这里标出了位置 -->
<param-value>/WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Log4jInitServlet.java :
package com.xxx.util;
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* to-do init log4j.
*/
public class Log4jInitServlet extends HttpServlet {
/**
* initial.
*/
public void init() {
String prefix = getServletContext().getRealPath("/");
// 读出上面标出的位置
String file = getInitParameter("log4j");
PropertyConfigurator.configure(prefix + file);
} // END: init
} // END: Log4jInitServlet
...
posted on 2006-11-21 17:10
周锐 阅读(986)
评论(0) 编辑 收藏 所属分类:
Apache 、
Java