β

Log4j配置

Arccode's blog 35 阅读

在Java Web项目中配置Log4j

示例文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "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="[%p] %d{yyyy-MM-dd HH:mm:ss:SSS} [%l] %m%n" />
</layout>
</appender>

<appender name="FILE-INFO" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/home/ad/logs/ad-quartz-info-db.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<param name="BufferSize" value="500" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{yyyy-MM-dd HH:mm:ss:SSS} [%l] %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="INFO" />
<param name="acceptOnMatch" value="false" />
</filter>
</appender>

<appender name="FILE-WARN" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/home/ad/logs/ad-quartz-warn-db.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<param name="BufferSize" value="500" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{yyyy-MM-dd HH:mm:ss:SSS} [%l] %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="FATAL" />
<param name="acceptOnMatch" value="false" />
</filter>
</appender>


<!-- Root Logger -->
<root>
<priority value="debug" />
<appender-ref ref="console" />
<appender-ref ref="FILE-INFO" />
<appender-ref ref="FILE-WARN" />
</root>

</log4j:configuration>

输出方式

org.apache.log4j.RollingFileAppender(滚动文件,自动记录最新日志)
org.apache.log4j.ConsoleAppender (控制台)
org.apache.log4j.FileAppender (文件)
org.apache.log4j.DailyRollingFileAppender (每天产生一个日志org.apache.log4j.WriterAppender (将日志信息以流格式发送到任意指定的地方)

日志优先级

OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG
Log4j建议只使用FATAL ,ERROR ,WARN ,INFO ,DEBUG这五个级别。

参数配置

# 格式说明layout中的参数都以%开始,后面不同的参数代表不同的格式化信息(参数按字母表顺序列出):
%p 输出优先级,即 FATAL ,ERROR 等
%d 输出日志时间其格式为 %d{yyyy-MM-dd HH:mm:ss,SSS},可指定格式 如 %d{HH:mm:ss}
%l 输出日志事件发生位置,包括类目名、发生线程,在代码中的行数
%n 换行符
%m 输出代码指定信息,如info(“message”),输出message
%c 输出所属类的全名,可在修改为 %c{Num} ,Num类名输出的维(如:"org.apache.elathen.ClassName",%c{2}将输出elathen.ClassName)
%r 输出从启动到显示该log信息所耗费的毫秒数
%t 输出产生该日志事件的线程名

在Java Web项目中配置Log4j

示例文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "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="[%p] %d{yyyy-MM-dd HH:mm:ss:SSS} [%l] %m%n" />
</layout>
</appender>

<appender name="FILE-INFO" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/home/ad/logs/ad-quartz-info-db.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<param name="BufferSize" value="500" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{yyyy-MM-dd HH:mm:ss:SSS} [%l] %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="INFO" />
<param name="acceptOnMatch" value="false" />
</filter>
</appender>

<appender name="FILE-WARN" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/home/ad/logs/ad-quartz-warn-db.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<param name="BufferSize" value="500" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d{yyyy-MM-dd HH:mm:ss:SSS} [%l] %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="FATAL" />
<param name="acceptOnMatch" value="false" />
</filter>
</appender>


<!-- Root Logger -->
<root>
<priority value="debug" />
<appender-ref ref="console" />
<appender-ref ref="FILE-INFO" />
<appender-ref ref="FILE-WARN" />
</root>

</log4j:configuration>
作者:Arccode's blog
原文地址:Log4j配置, 感谢原作者分享。

发表评论