log4j的xml配置方式

一,原来一贯用的properties配置文件
log4j.properties

log4j.rootLogger = WARN, stdout
#, file, html

#----------------------------------------------------------------
# Console
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.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方式的配置文件
log4j.xml 比较典型

<?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" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
</layout>
</appender>

<appender name="file" class="org.apache.log4j.FileAppender">
<param name="File" value="D:/test.log" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
</layout>
</appender>

<appender name="html" class="org.apache.log4j.FileAppender">
<param name="Append" value="true" />
<param name="File" value="D:/test.html" />
<layout class="org.apache.log4j.HTMLLayout" />
</appender>

<appender name="daily" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="log/daily.log" />
<param name="DatePattern" value="'daily.'yyyy-MM-dd'.log'" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" 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>


三,log4j的xml配置文件的树状结构如下所示,注意下图只显示了常用的部分。

xml declaration and dtd
|
log4j:configuration
|
+-- appender (name, class)
| |
| +-- param (name, value)
| +-- layout (class)
| |
| +-- param (name, value)
+-- logger (name, additivity)
| |
| +-- level (class, value)
| | |
| | +-- param (name, value)
| +-- appender-ref (ref)
+-- root
|
+-- param (name, class)
+-- level
| |
| +-- param (name, value)
+-- appender-ref (ref)

四,日志格式的含义
[list]
[*]%m 输出代码中指定的消息
[*]%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL
[*]%r 输出自应用启动到输出该log信息耗费的毫秒数
[*]%c 输出所属的类目,通常就是所在类的全名
[*]%t 输出产生该日志事件的线程名
[*]%n 输出一个回车换行符,Windows平台为"\r\n",Unix平台为"\n"
[*]%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式
[/list]

五,Log4j的初始化

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);
}
}
}


web.xml中配置一下

<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值