SpringBoot集成logback后访问日志端点
问题描述
使用SpringBootAdmin(sba)监控Springboot服务时,配置了logback日志框架,按天滚动生成日志,此时在sba的日志监控页面出现404,如下图所示:
解决方案
查看浏览器控制台以及查询sba文档可知,此处日志的显示主要是由spirngBoot的端点/actuator/logprofile控制日志显示的,因此从访问/actuator/logprofile端点开始解决。
创建一个测试项目
- 环境
IDEA
JDK 1.8
SpringBoot 2.3.3
- 引入meven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
引入web依赖是由于/actuator/logfile端点只有在web下才会生效
- 修改application.yml文件
spring:
application:
name: actuator-logfile
logging:
level:
root: info
com.lzm: debug
pattern:
file: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"
file:
# path: ./logs/${spring.application.name} # path和name设置一个即可,name的优先级高
name: ./logs/${spring.application.name}.log
management:
endpoints:
web:
exposure:
include: "*" # <1>
<1>处的代码表示开放所有的web端点,不开放的话会导致访问不到/actuator/logfile端点
访问/actuator/logfile端点
启动测试项目,浏览器输入/actuator/logfile,出现如下所示结果:
此时/actuator/logfile端点可以正常访问。
添加logback
- 引入logback文件如下
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds">
<contextName>logback</contextName>
<property name="log.path" value="logs" />
<property name="log.name" value="actuator-logfile" />
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<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}}" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/${log.name}.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/${log.name}.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>1</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
</filter>
</appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/${log.name}-error.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/${log.name}-error.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>1</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<springProfile name="dev">
<root level="debug">
<appender-ref ref="CONSOLE" />
<appender-ref ref="LOG_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
<!-- 4.2 生产环境:输出到文档 -->
<springProfile name="prod">
<root level="info">
<appender-ref ref="LOG_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<logger name="jdbc.sqlonly" level="OFF"></logger>
<logger name="jdbc.audit" level="INFO"></logger>
<logger name="jdbc.resultset" level="OFF"></logger>
<logger name="jdbc.connection" level="OFF"></logger>
<logger name="jdbc.sqltiming" level="OFF">
<appender-ref ref="CONSOLE" />
</logger>
<logger name="org.springframework.web.servlet" level="info" />
<logger name="org.springframework.data.mongodb.core" level="DEBUG"/>
</configuration>
- 修改logging配置
logging:
level:
root: info
pattern:
file: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"
# file: <1>
# path: ./logs/${spring.application.name}
# name: ./logs/${spring.application.name}.log
config: classpath:logback-custom.xml <2>
<1>处注释掉是由于使用了logback之后,file.path或者file.name已经失效了
<2>指定logback的配置文件
- 访问/actuator/logfile,出现如下界面:
此时竟然访问不到,这是什么鬼?
解决方案:
添加logback之后,需要额外添加一个配置,指定生成的日志文件才可以,配置如下:
management:
endpoint:
logfile:
external-file: logs/${spring.application.name}.log
但是有个问题,logback中配置的是滚动生成日志文件,每天生成的日志文件都带有后缀,因此需要在logback配置中显式指名生成的文件名称:
<file>${log.path}/${log.name}.log</file> <1>
添加上述配置后,虽然当天的日志文件不带有日志后缀,但是第二天生成新的日志文件之后,今天的就会带有日期后缀,不影响日志查看的同时也能满足端点访问。
总结
使用logback时,要指定具体的日志文件后才能给logfile端点访问到,否则不能正确访问。
具体的测试代码已上传gitee