Java中的Log4j简易配置

  |   0 评论   |   0 浏览

Java 中的 Log4j 简易配置

依赖(Maven)

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

配置(log4j.properties)

log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss.SSS} %t [%p] %c{1} (%F:%L) %m%n
#log4j.rootLogger=debug, stdout, R
#log4j.appender.R=org.apache.log4j.RollingFileAppender
#log4j.appender.R.File=firestorm.log
#log4j.appender.R.MaxFileSize=100KB
#log4j.appender.R.MaxBackupIndex=1
#log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss.SSS} %t [%p] %c{1} (%F:%L) %m%n

使用

import org.apache.log4j.Logger;
import org.junit.Test;

/**
 * @author ming
 * @version 1.0.0
 * @date 2020/6/23 15:07
 **/
public class MyTest {
    private static Logger log = Logger.getLogger(MyTest .class);

    @Test
    public void test() {
        System.out.println("hello");
        log.info("info");
        log.warn("warn");
        log.debug("debug");
        log.error("error");
    }
}

打印

hello
2020/06/28 15:35:18.193 main [INFO] MyTest (MyTest.java:18) info
2020/06/28 15:35:18.196 main [WARN] MyTest (MyTest.java:19) warn
2020/06/28 15:35:18.196 main [DEBUG] MyTest (MyTest.java:20) debug
2020/06/28 15:35:18.196 main [ERROR] MyTest (MyTest.java:21) error

标题:Java中的Log4j简易配置
作者:jockming112
地址:http://www.itwetouch.com/articles/2020/12/10/1607592208797.html