如何通过配置log4j.xml来打印指定类或指定级别的日志信息
发布网友
发布时间:2022-04-22 16:47
我来回答
共1个回答
热心网友
时间:2023-11-12 21:28
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- appender 部分是可用的日志输出方式定义,可以定义多个 -->
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %d{ISO8601}-- %p -- %m%n"/>
</layout>
</appender>
<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="e:/logs/debug.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %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="e:/logs/info.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %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="e:/logs/warn.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %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="e:/logs/error.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %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>
<appender name="WATCHTASK" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="e:/logs/watchtask.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %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>
<!-- category 部分定义了特定包或者类的输出级别和方式,可以有多个 -->
<category name="org.apache">
<priority value="warn"/>
<appender-ref ref="WARN"/>
</category>
<!-- 打印日志到固定的文件中WATCHTASK 选择自定义的日志级别设置 -->
<category name="com.test.task.WatchTimerTask">
<priority value="info"/>
<appender-ref ref="WATCHTASK"/>
</category>
<!-- root部分定义了log4j的默认输出级别和方式 -->
<root>
<priority value="warn"/>
<appender-ref ref="INFO"/>
<appender-ref ref="WARN"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
(1). 输出方式appender一般有5种:org.apache.log4j.RollingFileAppender(滚动文件,自动记录最新日志)org.apache.log4j.ConsoleAppender (控制台)??org.apache.log4j.FileAppender (文件)org.apache.log4j.DailyRollingFileAppender (每天产生一个日志文件)org.apache.log4j.WriterAppender (将日志信息以流格式发送到任意指定的地方)(2). 日记记录的优先级priority,优先级由高到低分为 OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL。Log4j建议只使用FATAL ,ERROR ,WARN ,INFO ,DEBUG这五个级别。(3). 格式说明layout中的参数都以%开始,后面不同的参数代表不同的格式化信息(参数按字母表顺序列出):%c 输出所属类的全名%d 输出日志时间其格式为 %d{yyyy-MM-dd HH:mm:ss,SSS},可指定格式 如 %d{HH:mm:ss}%l? 输出日志事件发生位置,包括类目名、发生线程,在代码中的行数%n 换行符%m 输出代码指定信息,如info(“message”),输出message%p 输出优先级,即 FATAL ,ERROR 等%r 输出从启动到显示该log信息所耗费的毫秒数%t 输出产生该日志事件的线程名