logback日志打印操作人

logback日志打印操作人

自定义拦截器

java 复制代码
package com.demo.dv.net.config;

import com.demo.dv.net.common.domain.UserInfo;
import com.demo.dv.net.common.utils.CurrentUserUtil;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (handler instanceof HandlerMethod) {
            //获取当前登录的用户
            UserInfo currentUser = CurrentUserUtil.getCurrentUser();
            MDC.put("user", currentUser.getFullUsername());
        }
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        MDC.clear();
    }
}

注册拦截器

java 复制代码
package com.demo.dv.net.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private AuthInterceptor authInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

    /**
     * {@inheritDoc}
     * <p>This implementation is empty.
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**"); //所有的请求都要拦截。
    }
}

配置logback文件

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="PROJECT_NAME" value="net"/>
    <property name="LOG_HOME" value="${LOG_PATH}"/>
    <property name="LOG_FILE"
              value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/all.${tomcat.port:-net}.log"/>
    <property name="LOG_FILE_ERR"
              value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/error.${tomcat.port:-net}.log"/>
    <property name="log_pattern"
              value="%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39}:%L : [TxId : %X{PtxId} , SpanId : %X{PspanId}] %X{user} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>

    <contextName>${PROJECT_NAME}</contextName>

    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <!--    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>-->
    <include resource="audit-elk-appender.xml"/>
    <jmxConfigurator/>

    <appender name="ALL_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_FILE}</file>
        <encoder>
            <pattern>
                ${log_pattern}
            </pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>512MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <appender name="ERR_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>
                ${log_pattern}
            </pattern>
        </encoder>
        <file>${LOG_FILE_ERR}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE_ERR}.%d{yyyy-MM-dd}.%i</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>512MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                ${log_pattern}
            </pattern>
            <charset>utf8</charset>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ALL_LOG"/>
        <appender-ref ref="ERR_LOG"/>
    </root>

    <!-- logger level统一在application-xx配置文件中设置 -->

</configuration>
相关推荐
FIN技术铺3 分钟前
Spring Boot框架Starter组件整理
java·spring boot·后端
小曲程序10 分钟前
vue3 封装request请求
java·前端·typescript·vue
陈王卜28 分钟前
django+boostrap实现发布博客权限控制
java·前端·django
小码的头发丝、28 分钟前
Spring Boot 注解
java·spring boot
午觉千万别睡过31 分钟前
RuoYI分页不准确问题解决
spring boot
java亮小白199733 分钟前
Spring循环依赖如何解决的?
java·后端·spring
飞滕人生TYF39 分钟前
java Queue 详解
java·队列
2301_811274311 小时前
大数据基于Spring Boot的化妆品推荐系统的设计与实现
大数据·spring boot·后端
武子康1 小时前
大数据-230 离线数仓 - ODS层的构建 Hive处理 UDF 与 SerDe 处理 与 当前总结
java·大数据·数据仓库·hive·hadoop·sql·hdfs
武子康1 小时前
大数据-231 离线数仓 - DWS 层、ADS 层的创建 Hive 执行脚本
java·大数据·数据仓库·hive·hadoop·mysql