码迷,mamicode.com
首页 > 其他好文 > 详细

Log4j之使用demo

时间:2019-02-17 00:47:46      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:eth   stdout   style   apache   deb   date   pre   port   microsoft   

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
</dependency>

 

log4j.properties

### 设置###
log4j.rootLogger = debug,stdout,debugLog,infoLog,errorLog

### 输出信息到控制抬 ###
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

### 输出DEBUG 级别以上的日志 ###
log4j.appender.debugLog = org.apache.log4j.DailyRollingFileAppender
log4j.appender.debugLog.File = E://logs/debug/log.log
log4j.appender.debugLog.DatePattern = ‘.‘yyyy-MM-dd-HH-mm‘.log‘
log4j.appender.debugLog.Append = true
log4j.appender.debugLog.Threshold = DEBUG
log4j.appender.debugLog.layout = org.apache.log4j.PatternLayout
log4j.appender.debugLog.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出INFO 级别以上的日志 ###
log4j.appender.infoLog = org.apache.log4j.DailyRollingFileAppender
log4j.appender.infoLog.File =E://logs/info/log.log
log4j.appender.infoLog.DatePattern = ‘.‘yyyy-MM-dd-HH-mm‘.log‘
log4j.appender.infoLog.Append = true
log4j.appender.infoLog.Threshold = INFO
log4j.appender.infoLog.layout = org.apache.log4j.PatternLayout
log4j.appender.infoLog.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志 ###
log4j.appender.errorLog = org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorLog.File =E://logs/error/log.log
log4j.appender.errorLog.DatePattern = ‘.‘yyyy-MM-dd-HH-mm‘.log‘
log4j.appender.errorLog.Append = true
log4j.appender.errorLog.Threshold = ERROR
log4j.appender.errorLog.layout = org.apache.log4j.PatternLayout
log4j.appender.errorLog.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

 

package com.zns;

import org.apache.log4j.Logger;

public class Test1 {
    private static Logger logger = Logger.getLogger(Test1.class);
    public static void main(String[] args) {
        logger.debug("hello world");
        //logger.info("hello world");
        //logger.error("hello world");
    }
}

 

 

SLF4J+Log4J整合

增加maven配置

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

 

 

最后,代码里声明logger要改一下,原来使用log4j是这样的

import org.apache.log4j.Logger;
class Test {
    final Logger logger = Logger.getLogger(Test.class);
    public void test() {
        logger.info("hello");
    }
}

 

现在要改成这样

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class Test {
    Logger logger = LoggerFactory.getLogger(Test.class);
    public void test() {
        logger.info("hello");
    }
}

 

Log4j之使用demo

标签:eth   stdout   style   apache   deb   date   pre   port   microsoft   

原文地址:https://www.cnblogs.com/zengnansheng/p/10389826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!