一,maven引入jar包
<!-- 日志文件管理包 --><!-- log4j start --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <!-- log4j end -->
二,使用:
import org.apache.log4j.Logger;
private final static Logger logger = Logger.getLogger(Test.class);
三,详细配置:
root日志
log4j.rootLogger = DEBUG, stdout, debug, info , warn, error
#控制台日志
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n
保存Debug信息到单独文件
log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File = C:\logs\log4j\debug.log
log4j.appender.debug.Append = true
###只输出DEBUG级别以上的日志!!!
log4j.appender.debug.Threshold = DEBUG
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n
log4j.appender.debug.DatePattern = '.'yyyy-MM-dd-HH-mm
###只输出DEBUG级别的日志!!!
log4j.appender.debug.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter
log4j.appender.debug.filter.infoFilter.LevelMin=DEBUG
log4j.appender.debug.filter.infoFilter.LevelMax=DEBUG
保存Info信息到单独文件
log4j.appender.info = org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.File = C:\logs\log4j\info.log
log4j.appender.info.Append = true
###只输出INFO级别以上的日志!!!
log4j.appender.info.Threshold = INFO
log4j.appender.info.layout = org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n
log4j.appender.info.DatePattern = '.'yyyy-MM-dd-HH-mm
###只输出INFO级别的日志!!!
log4j.appender.info.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter
log4j.appender.info.filter.infoFilter.LevelMin=INFO
log4j.appender.info.filter.infoFilter.LevelMax=INFO
保存Warn信息到单独文件
log4j.appender.warn = org.apache.log4j.DailyRollingFileAppender
log4j.appender.warn.File = C:\logs\log4j\warn.log
log4j.appender.warn.Append = true
###只输出WARN级别以上的日志!!!
log4j.appender.warn.Threshold = WARN
log4j.appender.warn.layout = org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n
log4j.appender.warn.DatePattern = '.'yyyy-MM-dd-HH-mm
###只输出WARN级别的日志!!!
log4j.appender.warn.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter
log4j.appender.warn.filter.infoFilter.LevelMin=WARN
log4j.appender.warn.filter.infoFilter.LevelMax=WARN
保存异常信息到单独文件
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File = C:\logs\log4j\error.log
log4j.appender.error.Append = true
###只输出ERROR级别以上的日志!!!
log4j.appender.error.Threshold = ERROR
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n
log4j.appender.error.DatePattern = '.'yyyy-MM-dd-HH-mm
###只输出ERROR级别的日志!!!
log4j.appender.error.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter
log4j.appender.error.filter.infoFilter.LevelMin=ERROR
log4j.appender.error.filter.infoFilter.LevelMax=ERROR
子日志.在指定类中专用,必须指定包类名
###指定日志名调用:private static Logger log1=Logger.getLogger("com.zjw.main.TestLogSon");
###指定包类名调用:private static Logger log1=Logger.getLogger(TestLogSon.class);
log4j.logger.com.zjw.main.TestLogSon=INFO,son,stdout
log4j.appender.son = org.apache.log4j.DailyRollingFileAppender
log4j.appender.son.File = C:\logs\log4j\son\TestLogSon.log
log4j.appender.son.Append = true
###只输出INFO级别以上的日志!!!
log4j.appender.son.Threshold = INFO
###不在root日志中输出,必须指定子日志全名
log4j.additivity.com.zjw.main.TestLogSon=false
log4j.appender.son.layout = org.apache.log4j.PatternLayout
log4j.appender.son.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n
log4j.appender.son.DatePattern = '.'yyyy-MM-dd-HH-mm
子日志.使用指定日志打印
###指定日志名调用:private static Logger log1=Logger.getLogger("log1");
log4j.logger.log1=INFO,son1,stdout
log4j.appender.son1 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.son1.File = C:\logs\log4j\son1\son1.log
log4j.appender.son1.Append = true
###只输出INFO级别以上的日志!!!
log4j.appender.son1.Threshold = INFO
###不在root日志中输出,必须指定子日志全名
log4j.additivity.log1=false
log4j.appender.son1.layout = org.apache.log4j.PatternLayout
log4j.appender.son1.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n
log4j.appender.son1.DatePattern = '.'yyyy-MM-dd-HH-mm
子日志.在指定目录中使用
###在指定目录中正常调用:private static Logger log1=Logger.getLogger(Test.class);
log4j.logger.com.zjw.main.test1=INFO,son2,stdout
log4j.appender.son2 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.son2.File = C:\logs\log4j\son2\son2.log
log4j.appender.son2.Append = true
###只输出INFO级别以上的日志!!!
log4j.appender.son2.Threshold = INFO
###不在root日志中输出,必须指定子日志全名
log4j.additivity.com.zjw.main.test1=false
log4j.appender.son2.layout = org.apache.log4j.PatternLayout
log4j.appender.son2.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n
log4j.appender.son2.DatePattern = '.'yyyy-MM-dd-HH-mm
###参考log4j配置详解【https://www.cnblogs.com/ITtangtang/p/3926665.html】
共同学习,写下你的评论
评论加载中...
作者其他优质文章