首先当然是得到log4j的jar档,推荐使用1.2.X版,下载地址:
http://logging.apache.org/log4j/1.2/download.html
xml格式的log4j配置文件概述
xml格式的log4j配置文件需要使用org.apache.log4j.xml.DOMConfigurator.configure()方法来读入。对xml文件的语法定义可以在log4j的发布包中找到:org/apache/log4j/xml/log4j.dtd。
log4j的xml配置文件的树状结构如下所示,注意下图只显示了常用的部分。
Java代码
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)
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="myConsole" class="org.apache.log4j.ConsoleAppender">
layout class="org.apache.log4j.PatternLayout">
param ="ConversionPattern" value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />
</layout>
<!--过滤器设置输出的级别-->
filter ="org.apache.log4j.varia.LevelRangeFilter"="levelMin"="debug" />
="levelMax"="warn" ="AcceptOnMatch"="true" filter>
appender>
="myFile"="org.apache.log4j.RollingFileAppender"="File"="D:/output.log" /> 设置日志输出文件名 -->
设置是否在重新启动服务时,在原有日志的基础添加新日志 ="Append"="MaxBackupIndex"="10" ="%p (%c:%L)- %m%n" ="activexAppender"="org.apache.log4j.DailyRollingFileAppender"="E:/activex.log" ="DatePattern"="'.'yyyy-MM-dd'.log'" >
="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" 指定logger的设置,additivity指示是否遵循缺省的继承机制-->
logger ="com.runway.bssp.activeXdemo" additivity="false"priority value ="info"appender-ref ref="activexAppender" />
logger 根logger的设置root="debug"="myConsole"="myFile">
log4j:configuration>
原文链接:https://www.f2er.com/xml/299226.html