log4j配置—将不同级别的日志写到不同的日志文件中

通过log4j.properties:
   
log4j.threshold=ALL
log4j.rootLogger=ALL, stdout, DebugAppender, InfoAppender, WarnAppender, ErrorAppender

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%c %d{ISO8601} -- %p -- %m%n

log4j.logger.DebugAppender.access=DEBUG
log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.File=C:\\logs\\app-debug.log
log4j.appender.DebugAppender.MaxFileSize=500KB
log4j.appender.DebugAppender.MaxBackupIndex=10
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%c %d{ISO8601} -- %p -- %m%n

log4j.logger.InfoAppender.access=INFO
log4j.appender.InfoAppender=org.apache.log4j.RollingFileAppender
log4j.appender.InfoAppender.File=C:\\logs\\app-info.log
log4j.appender.InfoAppender.MaxFileSize=500KB
log4j.appender.InfoAppender.MaxBackupIndex=10
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoAppender.layout.ConversionPattern=%c %d{ISO8601} -- %p -- %m%n

log4j.logger.WarnAppender.access=WARN
log4j.appender.WarnAppender=org.apache.log4j.RollingFileAppender
log4j.appender.WarnAppender.File=C:\\logs\\app-warn.log
log4j.appender.WarnAppender.MaxFileSize=500KB
log4j.appender.WarnAppender.MaxBackupIndex=10
log4j.appender.WarnAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.WarnAppender.layout.ConversionPattern=%c %d{ISO8601} -- %p -- %m%n

log4j.logger.ErrorAppender.access=ERROR
log4j.appender.ErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.ErrorAppender.File=C:\\logs\\app-error.log
log4j.appender.ErrorAppender.MaxFileSize=500KB
log4j.appender.ErrorAppender.MaxBackupIndex=10
log4j.appender.ErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.layout.ConversionPattern=%c %d{ISO8601} -- %p -- %m%n

注:此方法好像没有实现,需要大家继续努力去破解啦
(2)通过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="%c %d{ISO8601} -- %p -- %m%n"/>
</layout>
</appender>

<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:\\log\\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>

<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:\\log\\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>

<appender name="WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:\\log\\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>

<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:\\log\\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>

<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>

注:这个是可行的,实现啦将不同级别的日志写到不同的日志文件中。

 

Java类中的使用:

   private Log logger = LogFactory.getLog(SendSmsServlet.class);

   logger.info("短信MT流程开始-----------");

posted @ 2012-11-20 18:16  学无先后 达者为先  阅读(1072)  评论(0编辑  收藏  举报