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,出现以下打印日志,则表示成功。
、
版权声明:本文为kuangpengfei原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。