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

相关推荐
FFZero11 天前
[mpv插件系统] (一) Lua 闭包与上值 — 从概念到 C API
c语言·junit·lua
wb043072012 天前
厨房质检员——从阿明的“祖传配方“到标准化质检,看测试金字塔的落地
架构·log4j
汽车仪器仪表相关领域2 天前
Kvaser Hybrid CAN/LIN 单通道三合一总线分析仪:高性价比CAN FD/LIN集成测试利器
运维·服务器·网络·数据挖掘·数据分析·单元测试·集成测试
一路往蓝-Anbo2 天前
第十章:TDD部署 —— Ceedling 环境的深度集成
stm32·单片机·嵌入式硬件·单元测试·测试驱动开发·tdd
川石课堂软件测试3 天前
使用mock进行接口测试教程
数据库·python·功能测试·测试工具·华为·单元测试·appium
芒鸽4 天前
鸿蒙应用测试实战:从单元测试到自动化测试
华为·单元测试·harmonyos
MC皮蛋侠客5 天前
Google Test 单元测试指南
c++·单元测试·google test
英俊潇洒美少年6 天前
前端 Jest 单元测试零基础实战:模板、提效、避坑、面试题(Vue 项目可用)
前端·vue.js·单元测试
老码观察7 天前
设计模式实战解读(四):观察者模式——事件驱动的解耦利器
观察者模式·设计模式·log4j