log4j配置--根據日誌級別出入到不一樣路徑

一、配置相同級別的日誌出入到不一樣路徑 apache

log4j.rootLogger=DEBUG, A1,A2 app

#輸出到控制檯
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}[%-10C] %m  %n
# A1 uses PatternLayout.

#輸出到文件
log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=D:/temp/baits.log
log4j.appender.A2.Append=true
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}[%-10C] %m  %n spa

二、配置不一樣級別的日誌輸出到不一樣路徑 debug

DOMConfigurator.configure("resource/log4j.xml");
//PropertyConfigurator.configure("resource/log4j.properties");
使用此加載不一樣的配置 日誌

(1)、使用properties文件 xml

添加以下代碼 it

log4j.appender.A2.Threshold=INFO  io

(2)、使用xml文件的形式 class

<?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{yyyy-MM-dd HH:mm:ss}[%-10C] %m  %n"/>
</layout>
</appender>

<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="D:/temp/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="%-d{yyyy-MM-dd HH:mm:ss}[%-10C] %m  %n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="DEBUG" />
<param name="LevelMin" value="DEBUG" />
</filter>
</appender>

<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="D:/temp/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="%-d{yyyy-MM-dd HH:mm:ss}[%-10C] %m  %n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="INFO" />
<param name="LevelMin" value="INFO" />
</filter>
</appender>
<!-- 根logger的設置-->  
<root>
<appender-ref ref="STDOUT"/>
<appender-ref ref="DEBUG"/>
<appender-ref ref="INFO"/>
</root>
</log4j:configuration>
三、天天創建新日誌文件
配置

log4j.rootLogger=INFO,A1,A2 #log4j.rootLogger=INFO, A2 # A1 is set to be a ConsoleAppender which outputs to System.out. log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}[%-10C] %m  %n # A1 uses PatternLayout. #log4j.appender.A2=org.apache.log4j.ConsoleAppender #log4j.appender.A2.layout=org.apache.log4j.PatternLayout log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender log4j.appender.A2.File=${basketballGM.root}/logs/back.log log4j.appender.A2.DatePattern='.'yyyy-MM-dd log4j.appender.A2.layout=org.apache.log4j.PatternLayout log4j.appender.A2.Append=true log4j.appender.A2.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}[%-10C] %m  %n

相關文章
相關標籤/搜索