idea+maven+log4j

Java 发表评论


根据文章题目就可以清晰的知道,本博主要讲解在idea环境下,配合maven引入log4j日志功能。做法如下:
1 . 在maven的本地仓库中加入相应的jar包 —>

slf4j-log4j12-1.7.25.jar 

2 . 在xml配置文件中进行如下配置,加入log4j依赖。

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>2.10.0</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
</dependency>

3 .配置log4j.properties文件

    ### setting ###  
    log4j.rootLogger = INFO , console , debug , error  

    ### console ###  
    log4j.appender.console = org.apache.log4j.ConsoleAppender    
    log4j.appender.console.layout = org.apache.log4j.PatternLayout  
    log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} [%p]-[%c] %m%n  

    ### Log Error ###  
    log4j.appender.error = org.apache.log4j.DailyRollingFileAppender  
    log4j.appender.error.DataPattern = ','yyyy-MM-dd
    log4j.appender.error.File = ${catalina}/logs/comm.log 
    log4j.appender.error.Append = true  
    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} [%p]-[%c] %m%n  


    ### Log stdout ### 
    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{ABSOLUTE} %l %c%n%p: %m%n  

    ### Log debug ### 
    log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender  
    log4j.appender.debug.File = ${catalina}/logs/error.log 
    log4j.appender.debug.Append = true  
    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} [%p]-[%c] %m%n  

4 .在java文件中使用log4j日志功能:

private static Logger logger = Logger.getLogger(类.class);

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *