微服务系列之SpringBoot基础:自定义Starter记录日志

SpringBoot自定义Starter记录日志

一、项目结构

两个工程如下:customLog-spring-boot-autoconfigure、customLog-spring-boot-starter。每个空的starter都要依赖autoconfigure,即xxx-starter是一个空的项目,里面只是引入xxx-autoconfigure依赖。
在这里插入图片描述

二、autoconfigure项目pom文件添加依赖

customLog-spring-boot-autoconfigure工程:

<dependencies>

   <!-- 每个自定义starter都要依赖的基础依赖 -->
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
     </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

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

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

三、编写日志注解

customLog-spring-boot-autoconfigure工程:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {
    //接口方法上的描述信息
    String desc() default "";
}

四、编写日志拦截器

@Slf4j
public class LogInterceptor implements HandlerInterceptor {
    private static final  ThreadLocal th = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        LogAnnotation methodAnnotation = handlerMethod.getMethodAnnotation(LogAnnotation.class);
        if(methodAnnotation != null){
            long start = System.currentTimeMillis();
            th.set(start);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        LogAnnotation methodAnnotation = handlerMethod.getMethodAnnotation(LogAnnotation.class);
        if(methodAnnotation != null){
            Method method = handlerMethod.getMethod();
            String requestURI = request.getRequestURI();
            String methodName = method.getDeclaringClass().getName()+":"+method.getName();
            String desc = methodAnnotation.desc();
            long end = System.currentTimeMillis();
            long start = (long)th.get();
            long l = end - start;
            th.remove();
            log.info("请求路径:{},请求方法:{},描述信息:{},总计耗时:{}",requestURI,methodName,desc,l);
        }
    }
}
@Configuration
public class LogAutoConfiguration implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogInterceptor()).addPathPatterns("/api/**");
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}

五、编写spring.factories

在resources/META-INF/下建立spring.factories文件,配置自动装配类路径

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.customlog.config.LogAutoConfiguration

六、打包

分别对customLog-spring-boot-autoconfigure、customLog-spring-boot-starter工程进行install,安装到maven仓库。

七、starter项目pom文件添加依赖

customLog-spring-boot-starter 工程:

 <dependencies>
        <dependency>
            <groupId>com.customLog</groupId>
            <artifactId>customLog-spring-boot-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

八、在项目中使用自定义starter

新建项目和TestCustomStarter,如下:
在这里插入图片描述

@RestController
@RequestMapping("/api")
public class TestCustomStarter {

    @LogAnnotation
    @GetMapping("/test")
    public String test(){
        return "测试成果......";
    }
}

九、测试

启动测试项目,访问http://localhost:8080/api/test,出现以下打印日志,则表示成功。
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值