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;
        }
    }
  • 相关阅读:
    Java.io.outputstream.PrintStream:打印流
    Codeforces 732F. Tourist Reform (Tarjan缩点)
    退役了
    POJ 3281 Dining (最大流)
    Light oj 1233
    Light oj 1125
    HDU 5521 Meeting (最短路)
    Light oj 1095
    Light oj 1044
    HDU 3549 Flow Problem (dinic模版 && isap模版)
  • 原文地址:https://www.cnblogs.com/tiancai/p/14927131.html
Copyright © 2011-2022 走看看