欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

log4 slf4j使用(一)

程序员文章站 2023-12-25 16:23:09
...
  • 目标
    ·slf4j接口+log4j实现,完成记录日志
    ·lombok 使用@Slf4j注解,快速开发,免写 private static final Logger log = LoggerFactory.getLogger(MainTest.class);

  • code
    测试程序

package com.mvp.www;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @classDesc: 功能描述: slf4j和log4j/log4j2的结合,
 * log4j使用log4j.properties,log4j2使用log4j2.yml/log4j2-test.xml(正式项目中使用一种,同时调试两种,文件冲突,clean一下)
 */
@Slf4j
public class MainTest {
//    private static final Logger log = LoggerFactory.getLogger(MainTest.class);

    @Test
    public void test() {
        log.info("MainTest.test,Hello={}", "Hello");
        log.error("MainTest.test,Num={}", 100);
        log.warn("MainTest.test,Clear={}", "This error is clear");
    }
}

lombok maven依赖

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>

junit单元测试maven依赖

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>

一) slf4j + log4j
maven依赖

        <!--slf4j + log4j-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <!--2020稳定版本1.7.30, 2.0在测试,和slf4j-api保持一致-->
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <!--1.x版本到1.2.17不再更新-->
            <version>1.2.17</version>
        </dependency>

log4j的配置文件log4j.properties


# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

1.2.17, 1.x的截止版本
log4 slf4j使用(一)
二)slf4j + log4j2
maven依赖

        <!--slf4j + log4j2-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.12.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.12.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.12.1</version>
        </dependency>

配置文件log4j2-test.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

读取默认配置文件
log4 slf4j使用(一)

  • 官方文档
    ·http://logging.apache.org/log4j/2.x/
    ·http://logging.apache.org/log4j/1.2/
相关标签: 日志

上一篇:

下一篇: