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

相关推荐
hh9506 小时前
gent Plan × DeepSeek Harness Agent 单元测试与行为回归框架
人工智能·数据挖掘·回归·单元测试·agent plan·adg成都社区·adg社区
川石课堂软件测试13 小时前
自动化测试常见的异常处理
python·jmeter·mysql·docker·容器·单元测试·grafana
鲨鱼辣钊13 小时前
Github Copilot 研发效能提升实战指南
log4j·github·copilot
肠畔码农1 天前
深度解密 Redis 核心引擎:从 MULTI/EXEC 事务机制到 EVAL 脚本的原子性演进
数据库·redis·junit
智码看视界2 天前
Day 56:AI辅助开发全面提效:Copilot + Cursor的Java开发
java·单元测试·copilot·cursor·后端开发·代码审查·ai辅助开发
会编程的吕洞宾3 天前
Spring Cloud Gateway限流:Redis令牌桶防刷实战
数据库·redis·junit
深维AI随笔4 天前
《构建之法》| 第二章个人技术和流程:单元测试不是“可选“,而是“必需“
单元测试·log4j·apache
OPEN-F4 天前
Python进阶教程:单元测试与代码质量
开发语言·python·单元测试
難釋懷6 天前
Nginx-Lua
nginx·junit·lua