log4j日志级别
logger(日志记录器)有5个正常的日志级别,分别是Debug,Info,Warn,Error和Fatal,还有2个特殊的日志级别:All和Off。
这七个日志级别按优先级排序为:OFF,FATAL,ERROR,WARN,INFO,DEBUG,ALL,如果定义日志级别为INFO,则优先级低于INFO(DEBUG,ALL)的日志级别无效。
log4j.properties配置实例
log4j.rootLogger=debug, stdout, R, L
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.Threshold=DEBUG
log4j.appender.R.File=D:/ebooking.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n
log4j.appender.R.MaxFileSize=20MB
log4j.appender.L=org.apache.log4j.DailyRollingFileAppender
log4j.appender.L.Threshold=DEBUG
log4j.appender.L.File=D:/ebooking_error.log
log4j.appender.L.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.L.layout=org.apache.log4j.PatternLayout
log4j.appender.L.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n
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="A1" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="sshdemo2.log" />
<param name="MaxFileSize" value="1MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}][%C-%M]%m%n" />
</layout>
</appender>
<appender name="A2" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}][%C-%M]%m%n" />
</layout>
</appender>
<logger name="com.jjm.dao">
<level value="DEBUG" />
<appender-ref ref="A1" />
</logger>
<root>
<priority value="DEBUG" />
<appender-ref ref="A2" />
</root>
</log4j:configuration>