Slf4j+Log4j简单使用

Slf4j+Log4j简单使用

文章目录

  • Slf4j+Log4j简单使用
    • 一、引入依赖
    • [二、配置 log4j2.xml](#二、配置 log4j2.xml)
      • [2.1 配置结构](#2.1 配置结构)
      • [2.2 配置文件](#2.2 配置文件)
    • 三、使用
    • 四、使用MDC完成日志ID
      • [4.1 程序入口处](#4.1 程序入口处)
      • [4.2 配置文件配置打印](#4.2 配置文件配置打印)
      • [4.3 多线程日志ID传递配置](#4.3 多线程日志ID传递配置)
    • [五. 官网](#五. 官网)

一、引入依赖

xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.36</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.23.1</version>
    </dependency>
</dependencies>

二、配置 log4j2.xml

2.1 配置结构

2.2 配置文件

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config" status="TRACE" monitorInterval="30">
    <Properties>
        <Property name="PROJECT_NAME">log4j2-example</Property>
        <Property name="LOG_PATH">./data/logs</Property>
        <Property name="pattern">[%d{yyyy-MM-dd HH:mm:ss,SSS}] [%c{1.}::%M] [%t] [%p]: %m%n</Property>
    </Properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${pattern}"/>
        </Console>
        <RollingFile name="RollingFile" fileName="${LOG_PATH}/${PROJECT_NAME}.log"
                     filePattern="${LOG_PATH}/${date:yyyy-MM-dd}/${PROJECT_NAME}-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="${pattern}"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFile"/>
        </Root>
    </Loggers>
</Configuration>

三、使用

java 复制代码
package com.chenjiacheng.log.log4j2.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by chenjiacheng on 2024/4/11 22:59
 *
 * @author chenjiacheng
 * @since 1.0.0
 */
public class Log4j2Example {
    private static final Logger log = LoggerFactory.getLogger(Log4j2Example.class);
    public static void main(String[] args) {
        log.trace("hello,world");
        log.debug("hello,world");
        log.info("hello,world");
        log.warn("hello,world");
        log.error("hello,world");
    }
}

四、使用MDC完成日志ID

4.1 程序入口处

java 复制代码
MDC.put("logId", UUID.randomUUID().toString());

4.2 配置文件配置打印

xml 复制代码
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss,SSS}] [%c{1.}::%M] [%t] [%p] [%X{logId}]: %m%n"/>

%x{key}: 用于打印MDC内指定Key的值。

4.3 多线程日志ID传递配置

添加配置: resources/log4j2.component.properties

properties 复制代码
isThreadContextMapInheritable=true

五. 官网

Slf4j: Slf4j

Log4j2: Log4j2

相关推荐
许彰午14 天前
39_Java单元测试JUnit入门
java·junit·单元测试
骇客之技术15 天前
AutoLua:在安卓上写 Lua 脚本
android·junit·lua
果子耶耶15 天前
让大模型帮我写单元测试,5个模型的覆盖率和边界处理能力实测
chatgpt·单元测试
闪电悠米15 天前
黑马点评-Redis ZSet-实现关注 Feed 流
服务器·网络·数据库·redis·缓存·junit·lua
川石课堂软件测试16 天前
APP自动化测试|高级手势操作&toast操作
css·功能测试·测试工具·microsoft·fiddler·单元测试·harmonyos
周杰伦fans17 天前
记一次 Visual Studio 突然报错“未能加载 Microsoft.Internal.VisualStudio.Interop”的奇葩经历
microsoft·log4j·visual studio
laplaya17 天前
C++大型项目组件通信与依赖管理实践
c++·log4j·apache
福大大架构师每日一题17 天前
ollama v0.30.7 正式发布:Hermes 桌面端落地,接口、文档、底层依赖全方位优化
golang·log4j
Thecozzy18 天前
单元测试 vs 手工测试:以水印功能为例
单元测试