如何使用log4j输出单个级别的log到指定文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用log4j输出单个级别的log到指定文件相关的知识,希望对你有一定的参考价值。
这种情况下,就需要debug、info、warn、error分别进行配置了。之后通过priority属性控制日志实际输出级别就可以。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-dyyyy-MM-dd HH:mm:ss,SSS||||%p||||%m%n" />
</layout>
</appender>
<!-- 输出debug日志 -->
<appender name="debug.log" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/tmrpt/log/debug.log" />
<param name="Append" value="true" />
<param name="encoding" value="UTF-8"/>
<param name="DatePattern" value="yyyyMMddHH"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-dyyyy-MM-dd HH:mm:ss,SSS [%p][%c]- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="debug" />
<param name="levelMax" value="debug" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<!-- 输出info日志 -->
<appender name="info.log" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/tmrpt/log/info.log" />
<param name="Append" value="true" />
<param name="encoding" value="UTF-8"/>
<param name="DatePattern" value="yyyyMMddHH"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-dyyyy-MM-dd HH:mm:ss,SSS [%p][%c]- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="info" />
<param name="levelMax" value="info" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<!-- 输出warn日志 -->
<appender name="warn.log" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/tmrpt/log/warn.log" />
<param name="Append" value="true" />
<param name="encoding" value="UTF-8"/>
<param name="DatePattern" value="yyyyMMddHH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-dyyyy-MM-dd HH:mm:ss,SSS [%p][%c]- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="warn" />
<param name="levelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<!-- 输出error日志 -->
<appender name="error.log" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/tmrpt/log/error.log" />
<param name="Append" value="true" />
<param name="encoding" value="UTF-8"/>
<param name="DatePattern" value="yyyyMMddHH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-dyyyy-MM-dd HH:mm:ss,SSS [%p][%c]- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="error" />
<param name="levelMax" value="error" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<!-- Root Logger -->
<root>
<!-- priority配置输出日志级别 -->
<priority value="info" />
<appender-ref ref="console" ></appender-ref>
<appender-ref ref="info.log" />
<appender-ref ref="debug.log" />
<appender-ref ref="warn.log" />
<appender-ref ref="error.log"/>
</root>
</log4j:configuration> 参考技术A Log4j的配置功能是十分强大的,将Log分别输出到文件和屏幕,分别控制显示的级别(比如屏幕输出INFO级信息,文件输出ERROR级信息),一个很有用的关键字"Threshold",通过分别指定Threshold来控制输出的信息级别,具体内容如下:
log4j.rootLogger=INFO,A1,A2
#for Console
log4j.appender.A1.Threshold=INFO
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %m %n#for Filelog4j.appender.A2.Threshold=ERROR
log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=Scan.log
log4j.appender.A2.Append=true本回答被提问者和网友采纳
以上是关于如何使用log4j输出单个级别的log到指定文件的主要内容,如果未能解决你的问题,请参考以下文章