zoukankan      html  css  js  c++  java
  • springboot 切面日志记录

    package com.web.aspect;
    
    
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.MDC;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.*;
    
    
    /**
     */
    @Slf4j
    @Aspect
    @Configuration
    public class LogAspect {
    
        @Pointcut("execution(* com.web.controller..*.*(..))")
        private void controllerAspect(){
        }
    
        @Before(value = "controllerAspect()")
        public void invokeBefore(JoinPoint point) {
            String realClassName = getRealClassName(point);
            String traceId = UUID.randomUUID().toString().replace("-", "");
            MDC.put("traceId", traceId);
            if (canPrintLog(point)) {
                log.info("invoke class: {}, execute method: {}, requestParams: {},header: {}", realClassName, getMethodName(point), getRequestParams(point),getRequestHeaderInfo());
            }
        }
    
        @AfterReturning(pointcut = "controllerAspect()",returning="returnValue")
        public void invokeAfter(JoinPoint point, Object returnValue) {
            String realClassName = getRealClassName(point);
            if (canPrintLog(point)) {
                log.info("invoke class: {}, execute method: {}", realClassName, getMethodName(point));
            }
            MDC.clear();
        }
    
    
        private String getRealClassName(JoinPoint point) {
            return point.getTarget().getClass().getName();
        }
    
        private String getMethodName(JoinPoint point) {
            return point.getSignature().getName();
        }
    
    
        private List<Object> getRequestParams(JoinPoint point) {
            List<Object> params = new ArrayList<>();
            Object[] args = point.getArgs();
            for (Object arg: args) {
                if (arg instanceof PropertyUser) {
                    PropertyUser obj = new PropertyUser();
                    PropertyUser user = (PropertyUser)arg;
                    obj.setUserId(user.getUserId());
                    obj.setUserName(user.getUserName());
                    params.add(obj);
                } else {
                    params.add(arg);
                }
            }
            return params;
        }
    
        private boolean canPrintLog(JoinPoint point) {
            List<String> canNotPrintLog = new ArrayList<>();
            return !canNotPrintLog.contains(point.getSignature().getName());
        }
    
        private Map<String,String> getRequestHeaderInfo(){
            ServletRequestAttributes sra =  (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = sra.getRequest();
            Enumeration<String> enumeration = request.getHeaderNames();
            Map<String,String> headerMap = Maps.newHashMap();
            while (enumeration.hasMoreElements()) {
                String name = enumeration.nextElement();
                if(!"cookie".equals(name)){
                    headerMap.put(name,request.getHeader(name));
                }
            }
            return headerMap;
        }
    }
  • 相关阅读:
    Docker服务启动报错:Job for docker.service failed because the control process exited with error code.
    mysql忘记密码如何重置及修改密码
    linux下的/opt目录作用
    linux防火墙查看状态firewall、iptable
    nmap基本使用方法
    HTTP响应码大全
    端口镜像
    查看占用端口
    restful规范 APIview 解析器组件 Postman
    状态码301和302的区别
  • 原文地址:https://www.cnblogs.com/tiancai/p/14927131.html
Copyright © 2011-2022 走看看