springboot集成plumelog-lite日志管理
一个超级宝藏的开源日志管理系统:Plumelog: 一个简单易用的java日志系统,解放你的日志查询困难问题,方便快速追踪问题,安装配置简单,性能优秀 演示视频地址:https://v.qq.com/x/page/g3308uxlcnw.html (gitee.com)
该开源项目分有plumelog(完整版)和plumelog-lite(简洁版)
lite版更适合个人开发者或公司小型项目等,集成更为方便快捷
- 先引入依赖,在pom.xml中添加plumelog-lite依赖,官方文档写的添加3.5.3的依赖(最新吧),但亲测有点问题,滚动日志连不上,改成3.5.2就可以了
<dependency><groupId>com.plumelog</groupId><artifactId>plumelog-lite-spring-boot-starter</artifactId><version>3.5.2</version>
</dependency>
- 在springboot项目的resources目录下添加logback.xml配置文件,其中应用名称和日志保存位置根据自己情况自定义
<?xml version="1.0" encoding="UTF-8" ?>
<configuration><!-- 引入spirng boot默认的logback配置文件 --><include resource="org/springframework/boot/logging/logback/defaults.xml"/><!-- 彩色日志格式 --><property name="CONSOLE_LOG_PATTERN"value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/><!-- console --><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>${CONSOLE_LOG_PATTERN}</pattern><charset>utf-8</charset></encoder><filter class="ch.qos.logback.classic.filter.ThresholdFilter"><level>INFO</level></filter></appender><appender name="plumelog" class="com.plumelog.lite.logback.appender.LiteAppender"><!-- 应用名称 --><appName>app-demo</appName><!-- 日志存储位置 --><logPath>./plumelog/lite</logPath><!-- 日志保留天数 --><keepDay>30</keepDay></appender><!-- 添加 ref--><root level="INFO"><appender-ref ref="plumelog"/><appender-ref ref="STDOUT"/></root>
</configuration>
- 还需要加一个config配置类,不然拦截器会被覆盖,追踪码就拦截不到显示不出来
import com.plumelog.core.PlumeLogTraceIdInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration
public class TraceIdInterceptorsConfig extends WebMvcConfigurerAdapter{private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {//就是这句addResourceLocations,加上静态文件访问路径registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new PlumeLogTraceIdInterceptor());super.addInterceptors(registry);}}
-
启动项目,访问你的项目地址+plumelog/#/访问,例如:http://localhost:8083/plumelog/#/ 一定要加这个/#/后缀
-
其他异常可以参考plumelog-lite的说明文档:plumelog-lite/README.md · plume开源社区/Plumelog - Gitee.com
- 下面是一些效果图