ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

日志系列---【SpringBoot使用Aop实现格式化日志】

2021-11-16 01:32:37  阅读:185  来源: 互联网

标签:info SpringBoot ApiOperation --- org import 日志 annotation log


1.最终实现效果

 

2.在pom中引入Aop依赖(可以先写个@Aspect注解,如果不报错,说明项目中引入过aop依赖了,不用再重复引入下面的依赖)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

3.这里我项目中用了swagger,为了不重复写注释,我直接用了@ApiOperation注解作为切点,当然,也可以自定义注解,把ApiOperation换成自定义的就行了。

package com.system.annotation.Handler;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
public class LogHandler {

    @Pointcut("@annotation(io.swagger.annotations.ApiOperation)")
    public void webLog() {
    }

    @Around("webLog()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = proceedingJoinPoint.proceed();
        //打印出参
        log.info("Response Args : {}", result);
        //执行耗时
        log.info("exe-time      : {} ms", System.currentTimeMillis() - startTime);
        log.info("============================================End==================================================="+System.lineSeparator());
        return result;
    }

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        ApiOperation annotation = method.getAnnotation(ApiOperation.class);
        String msg = annotation.value();
        log.info(System.lineSeparator());
        log.info("============================================Start=================================================");
        //打印请求的url
        log.info("URL           : {}",request.getRequestURL().toString());
        log.info("Description   : {}",msg);
        log.info("HTTP Method   : {}",request.getMethod());
        log.info("Class Method  : {},{}",joinPoint.getSignature().getDeclaringTypeName(),joinPoint.getSignature().getName());
        log.info("IP            : {}",request.getRemoteAddr());
        Object[] args = joinPoint.getArgs();
        log.info("Request Args  : {}",args);
    }

    @After("webLog()")
    public void doAfter() throws Throwable{
        log.info("==========================================Response=================================================");
    }
}

4.自定义注解(可以不自定义,我直接用的ApiOperation)

package com.jiulong.springboot_validator.annotation;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface WebLog {

    /**
     * 日志描述信息
     *
     * @return String
     */
    String value() default "";
}

标签:info,SpringBoot,ApiOperation,---,org,import,日志,annotation,log
来源: https://www.cnblogs.com/hujunwei/p/15559298.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有