1、首先加入依赖

  1.  
    <!– https://mvnrepository.com/artifact/org.slf4j/slf4j-api –>
  2.  
    <dependency>
  3.  
    <groupId>org.slf4j</groupId>
  4.  
    <artifactId>slf4j-api</artifactId>
  5.  
    <version>1.7.26</version>
  6.  
    </dependency>
  7.  
    <!– https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 –>
  8.  
    <dependency>
  9.  
    <groupId>org.slf4j</groupId>
  10.  
    <artifactId>slf4j-log4j12</artifactId>
  11.  
    <version>1.7.26</version>
  12.  
    </dependency>

注意scope的值,直接从mvn仓库复制过来的scope值是test,要把这个去了,否则只有测试的时候才会启用这俩依赖

2、接下来在src/main/resources目录下面创建log4j.properties配置文件,内容如下,具体格式可百度:

  1.  
    log4j.rootLogger = debug,stdout,D,E
  2.  
     
  3.  
    log4j.appender.stdout = org.apache.log4j.ConsoleAppender
  4.  
    log4j.appender.stdout.Target = System.out
  5.  
    log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
  6.  
    log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
  7.  
     
  8.  
    log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
  9.  
    log4j.appender.D.File = log.log
  10.  
    log4j.appender.D.Append = true
  11.  
    log4j.appender.D.Threshold = DEBUG
  12.  
    log4j.appender.D.layout = org.apache.log4j.PatternLayout
  13.  
    log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] – [ %p ] %m%n
  14.  
     
  15.  
    log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
  16.  
    log4j.appender.E.File = error.log
  17.  
    log4j.appender.E.Append = true
  18.  
    log4j.appender.E.Threshold = ERROR
  19.  
    log4j.appender.E.layout = org.apache.log4j.PatternLayout
  20.  
    log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] – [ %p ] %m%n

3、在需要使用日志的地方引入如下代码

  1.  
    Logger logger = LoggerFactory.getLogger(MainGet.class);
  2.  
    logger.info(“具体日志内容”);

 

转载于:https://my.oschina.net/u/222749/blog/3074034