美文网首页
好用的切面 打印web 日志

好用的切面 打印web 日志

作者: 川流不息attitude | 来源:发表于2022-07-11 11:26 被阅读0次
@Aspect
@Component
@Slf4j
public class WebLogAspect {
    
    @Pointcut("@within(org.springframework.web.bind.annotation.RestController ) || @within(org.springframework.stereotype.Controller)")
    public void webLog(){}

    @Around("webLog()")
    public Object arround(ProceedingJoinPoint pjp) throws Throwable {
        Long start = System.currentTimeMillis();
        try {
            // 接收到请求,记录请求内容
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            String ua = request.getHeader("User-Agent");
            UserAgent userAgent = UserAgent.parseUserAgentString(ua);
            Browser browser = userAgent.getBrowser();
            OperatingSystem os = userAgent.getOperatingSystem();
            String system = os.getName();
            String browserName = browser.getName();
            log.info("browser:{} system:{}", browserName,system);
            String contentType = request.getHeader("content-type");
            log.info("content-type:{}",  contentType);
            log.info("request ip:{}", ServletUtil.getClientIP(request));
            log.info("http_method : {}", request.getMethod());
            log.info("uri : {}", request.getRequestURI());

            log.info("parameters :{}", JacksonTool.toJsonNotNull(getRequestParams(pjp)));

            //方便查找那台服务器报错
            InetAddress addr = InetAddress.getLocalHost();
            String ip = addr.getHostAddress();
            String hostName = addr.getHostName();

            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String principal = "";
            if(null != authentication){
                principal = authentication.getName();
            }
            log.info("username : {} ", principal);
            log.info("host : {} ip {}", hostName, ip);
            Object result = pjp.proceed();
            return result;
        } catch (Throwable e) {
            throw e;
        } finally {
            Long end = System.currentTimeMillis();
            log.info("耗时:{}秒", (end - start) / 1000);
        }
    }

    /**
     * 获取入参
     * @param proceedingJoinPoint
     *
     * @return
     * */
    private Map<String, Object> getRequestParams(ProceedingJoinPoint proceedingJoinPoint) {
        Map<String, Object> requestParams = new HashMap<>();

        //参数名
        String[] paramNames =
                ((MethodSignature)proceedingJoinPoint.getSignature()).getParameterNames();
        //参数值
        Object[] paramValues = proceedingJoinPoint.getArgs();

        for (int i = 0; i < paramNames.length; i++) {
            Object value = paramValues[i];

            //如果是文件对象
            if (value instanceof MultipartFile) {
                MultipartFile file = (MultipartFile) value;
                //获取文件名
                value = file.getOriginalFilename();
            }
            if(value instanceof HttpServletResponse){
                continue;
            }
            if(value instanceof HttpServletRequest){
                continue;
            }
            requestParams.put(paramNames[i], value);
        }

        return requestParams;
    }


}

相关文章

网友评论

      本文标题:好用的切面 打印web 日志

      本文链接:https://www.haomeiwen.com/subject/vthnbrtx.html