IDEA 配置log4j

在这里插入图片描述

1.在pom.xml中添加maven依赖

        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>

2.在resource中添加log4j.properties文件

在这里插入图片描述

3.log4j配置如下

控制台只输出error,我比较推荐的配置,干净易查看。

log4j.rootLogger=DEBUG,stdout,I,D,E

#控制台输出error日志
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold = ERROR
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

#priority:ERROR,WARN,INFO,DEBUG
#if rootLogger is set to ERROR,info/debug will not be save,only error can be saved.
### 输出info error到 to D://logs/info.log ###
log4j.appender.I = org.apache.log4j.DailyRollingFileAppender  
log4j.appender.I.File = F://GXHS_TK_RAILWAY_SRC/railway_locomotives_moving_sets_project_code/tk_project/etl/logs/info.log

log4j.appender.I.Append = true 
log4j.appender.I.Threshold = INFO   
log4j.appender.I.layout = org.apache.log4j.PatternLayout  
log4j.appender.I.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n  

### 输出debug到 D://logs/log.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender  
log4j.appender.D.File = F://GXHS_TK_RAILWAY_SRC/railway_locomotives_moving_sets_project_code/tk_project/etl/logs/debug.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  

### 输出error到 D://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender  
log4j.appender.E.File =F://GXHS_TK_RAILWAY_SRC/railway_locomotives_moving_sets_project_code/tk_project/etl/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{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n   

你可能感兴趣的:(#,--IDEA,log4j)