Log4j按级别输出到不同的日志文件

<?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">
<!-- 布局格式
org.apache.log4j.HTMLLayout(以HTML表格形式布局),
org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
-->
<param name="ConversionPattern" value="%c %d{ISO8601}-- %p -- %m%n"/>
</layout>
</appender>

<!-- 只输出debug级别的日志信息 -->
<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<!-- 日志文件存储的路径 -->
<param name="File" value="/home/denghaiping/logs/app-debug.log"/>
<param name="Append" value="true"/>
<!-- 控制日志文件的大小 -->
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{ISO8601}-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>

<!-- 只输出info级别的日志信息 -->
<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/home/denghaiping/logs/app-info.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{ISO8601}-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>

<!-- 只输出WARN级别的日志信息 -->
<appender name="WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/home/denghaiping/logs/app-warn.log"/>
<param name="Append" value="true"/>
<!-- 每个日志文件大小 -->
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{ISO8601}-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>

<!-- 只输出error级别的日志信息 -->
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/home/denghaiping/logs/app-error.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{ISO8601}-- %p -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>

<!--
配置输出日志的级别
FATAL      0 
ERROR      3 
WARN       4 
INFO       6 
DEBUG      7
-->
<root>
<priority value="DEBUG"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="DEBUG"/>
<appender-ref ref="INFO"/>
<appender-ref ref="WARN"/>
<appender-ref ref="ERROR"/>
</root>

</log4j:configuration>

你可能感兴趣的:(apache,C++,c,log4j,C#)