码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot用SpringAOP实现页面日志访问功能

时间:2019-07-14 18:00:28      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:net   logger   method   java   mic   ==   ringbuf   str   ali   

每一个页面写请求日志太麻烦了,用AOP很方便的实现日志记录功能


@Aspect
@Component
public class LogAspect {
    private final static Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public * com.bao.cms.controller..*.*(..))")
    public void controllerMethod() {
    }
    @Before("controllerMethod()")
    public void LogRequestInfo(JoinPoint joinPoint) throws Exception {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        StringBuffer requestLog = new StringBuffer();
        requestLog.append("request:")
                .append("URL = {" + request.getRequestURI() + "},\t")
                .append("HTTP_METHOD = {" + request.getMethod() + "},\t")
                .append("IP = {" + request.getRemoteAddr() + "},\t")
                .append("CLASS_METHOD = {" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + "},\t");

        if(joinPoint.getArgs().length == 0) {
            requestLog.append("ARGS = {} ");
        } else {
            requestLog.append("ARGS = " + new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL)
                    .writeValueAsString(joinPoint.getArgs()[0]) + "");
        }

        LOGGER.info(requestLog.toString());
    }



}

 

 

 

技术图片

 

参考

https://www.cnblogs.com/wangshen31/p/9379197.html

https://blog.csdn.net/qq_35206261/article/details/81945618

SpringBoot用SpringAOP实现页面日志访问功能

标签:net   logger   method   java   mic   ==   ringbuf   str   ali   

原文地址:https://www.cnblogs.com/Guroer/p/11184849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!