首页 > 其他分享 >log4j2.xml配置自定义参数 日志变量打印,比如全局traceId

log4j2.xml配置自定义参数 日志变量打印,比如全局traceId

时间:2023-01-12 14:46:47浏览次数:49  
标签:xml traceId 自定义 TRACE MDC import public

1、在拦截器中 设置MDC 的变量

package com.sleep.demo.intercepter;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Slf4j
public class TraceInterceptor implements HandlerInterceptor {
    public static final String TRACE = "_trace";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String traceId = request.getHeader(TRACE);
        if (StringUtils.isBlank(traceId)) {
            traceId = UUID.randomUUID().toString().toUpperCase();
        }
        MDC.put("TRACE_ID", traceId);
        return true;
    }
}

2、在slf4j2.xml中配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <properties>

        <!-- 文件输出格式 -->
        <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [%thread][%X{TRACE_ID}] - [%X{requestId}] %c [%L] -| %msg%n</property>

        <!-- 日志根目录 -->
        <property name="BASEDIR">D://mylog</property>
        <Property name="rollingLogSize">1000 MB</Property>
        <Property name="rollingLogMaxNum">10</Property>
    </properties>

    <appenders>
        <Console name="CONSOLE" target="system_out">
            <PatternLayout pattern="${PATTERN}" />
        </Console>
        <!-- 所有日志 -->
        <RollingRandomAccessFile fileName="${BASEDIR}/all.log" filePattern="${BASEDIR}/%d{yyyyMMdd}/all.%d{yyyyMMdd}-%i.log"
                                 immediateFlush="true" name="ALL_FILE">
            <PatternLayout pattern="${PATTERN}"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="${rollingLogSize}"/>
            </Policies>
            <DefaultRolloverStrategy max="${rollingLogMaxNum}">
                <Delete basePath="${BASEDIR}" maxDepth="2">
                    <IfFileName glob="all.*.log"/>
                    <IfLastModified age="5D"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>
        <!-- ERROR级别日志 -->
        <RollingRandomAccessFile fileName="${BASEDIR}/error.log" filePattern="${BASEDIR}/%d{yyyyMMdd}/error.%d{yyyyMMdd}-%i.log"
                                 immediateFlush="true" name="ERROR_FILE">
            <Filters>
                <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="${PATTERN}"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="${rollingLogSize}"/>
            </Policies>
            <DefaultRolloverStrategy max="${rollingLogMaxNum}">
                <Delete basePath="${BASEDIR}" maxDepth="2">
                    <IfFileName glob="error.*.log"/>
                    <IfLastModified age="5D"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile fileName="${BASEDIR}/warning.log" filePattern="${BASEDIR}/%d{yyyyMMdd}/warning.%d{yyyyMMdd}-%i.log"
                                 immediateFlush="true" name="WARN_FILE">
            <Filters>
                <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="${PATTERN}"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="${rollingLogSize}"/>
            </Policies>
            <DefaultRolloverStrategy max="${rollingLogMaxNum}">
                <Delete basePath="${BASEDIR}" maxDepth="2">
                    <IfFileName glob="error.*.log"/>
                    <IfLastModified age="5D"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>
    </appenders>

    <loggers>
        <logger name="org.springframework" level="WARN" />
        <logger name="org.apache" level="WARN" />
        <logger name="io.netty" level="WARN" />

        <root level="DEBUG">
            <!-- 若不注释该打印,将可能会出现两次打印的情况,调试时使用 -->
            <appenderref ref="CONSOLE" />
            <appenderref ref="ALL_FILE" />
            <appenderref ref="WARN_FILE" />
            <appender-ref ref="ERROR_FILE"/>
        </root>
    </loggers>

</configuration>

 

标签:xml,traceId,自定义,TRACE,MDC,import,public
From: https://www.cnblogs.com/niun/p/17046600.html

相关文章