主要参考资料:
(推荐)java日志框架知识:https://www.cnblogs.com/gavanwanggw/p/7305218.html
log4j配置详解:https://blog.csdn.net/sinat_30185177/article/details/73550377
包或类的日志配置:https://www.cnblogs.com/yidaxia/p/5820036.html
Log4j迁移到Logback:https://blog.csdn.net/abc86319253/article/details/48731297
这里,以log4j为例,主要探究以下几个问题:
1、更改配置文件名字(log4j.properties),是否可以正常读取配置文件?
2、如何为单独一个类或者一个包配置日志?
3、additivity属性有什么作用?
4、appender的"Threshold = ERROR "和rootLogger的level有什么区别,同时配置会有什么效果?
5、如何实现不同日志系统的桥接?
一、环境配置:
引入jar包(log4j-1.2.17.jar)后,创建配置文件(log4j.properties)如下:
### \u8BBE\u7F6E###
log4j.rootLogger = info,stdout,D,E
### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###
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 = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### \u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E\://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern =%-d