log4j的xml配置方式
一,原来一贯用的properties配置文件
log4j.properties
log4j.rootLogger = WARN, stdout#, file, html#----------------------------# Consolelog4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n#----------------------------# File#log4j.appender.file = org.apache.log4j.FileAppender#log4j.appender.file.File = d:/mylog.log#log4j.appender.file.layout = org.apache.log4j.PatternLayout#log4j.appender.file.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n#----------------------------# HTML#log4j.appender.html = org.apache.log4j.FileAppender#log4j.appender.html.File = d:/mylog.html#log4j.appender.html.layout = org.apache.log4j.HTMLLayout#----------------------------# Package !!!#log4j.logger.com.ying.dang.actions=DEBUG
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"><appender name="stdout" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" /></layout></appender><appender name="file" value="D:/test.log" /><param name="Append" value="true" /><layout value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" /></layout></appender><appender name="html" value="true" /><param name="File" value="D:/test.html" /><layout /></appender><appender name="daily" value="log/daily.log" /><param name="DatePattern" value="'daily.'yyyy-MM-dd'.log'" /><param name="Append" value="true" /><layout value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" /> </layout></appender><category name="cn.com.legendapl.spring.log" additivity="false"> <level value="DEBUG" /><appender-ref ref="stdout" /></category><root><level value="ERROR" /><appender-ref ref="stdout" /><appender-ref ref="file" /><appender-ref ref="html" /></root></log4j:configuration>
public class Log4jInit extends HttpServlet { public void init() { String prefix = getServletContext().getRealPath("/"); String file = getInitParameter("Log4j-init-file"); if(file != null) { PropertyConfigurator.configure(prefix+file); } }}
<servlet> <servlet-name>Log4j-init</servlet-name> <servlet-class>xx.xx.Log4jInit</servlet-class> <init-param> <param-name>Log4j-init-file</param-name> <param-value>WEB-INF/classes/Log4j.properties</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet>1 楼 wait8712 2012-05-23 配置的太多了,还是properties配置文件比较好用,只是你的功能太少了,呵呵 2 楼 yingzhor 2012-05-23 我还写过这样一篇博客啊。 我自己都不记得了。