以 log4j-1.2.17 为例

1. 首先引入 log4j-1.2.17

2. java 代码中加载 log4j.properties 配置

String basicPath = System.getProperty("user.dir");
 // 日志配置
PropertyConfigurator.configure(basicPath+"\\Example\\src\\resource\\log4j.properties");

 

3. log4j.properties 配置,以下日志仅写控制台

log4j.rootLogger=debug,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%m%n
log4j.appender.console.Threshold=debug
log4j.appender.console.ImmediateFlush=TRUE
log4j.appender.console.Target=System.out

 

 log4.properties,日志写文件 

log4j.rootLogger=debug,console
log4j.appender.console=org.apache.log4j.RollingFileAppender
log4j.appender.console.File=./logs/debug.log
log4j.appender.console.MaxFileSize=50MB
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.MaxBackupIndex=10
log4j.appender.console.layout.ConversionPattern=[%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%m%n%n

 

posted on 2022-08-07 17:45  wisdo  阅读(242)  评论(0编辑  收藏  举报