好看的spring boot 日志配色(今天给大家露一手自定义springboot个性化定制日志)
好看的spring boot 日志配色(今天给大家露一手自定义springboot个性化定制日志)文档中说了使用:输出控制台日志颜色使用%clr(){blue}来改变颜色 ()中配置打印表达式The following colors and styles are supported:支持以下颜色这段话说的是springboot内部日志系统使用的是 Commons Logging 并且 SpringBoot 给 JDKLogging Log4j2(Log4j也是支持的) Logback 都提供了默认配置,并且如果使用了 Starters ,那么默认使用 Logback 。有这样一段话If your terminal supports ANSI color output will be used to aid readability. You can set spring.output.ansi.enabled to a supported value to override t
本文纯手写,喜欢关注一下我哦我们今天要做的就是,实现自定义个性化springboot日志。就是控制springboot打印在控制台日志的内容美观,这样方便我们看清楚日志的各个内容!
请看下面效果图!
地址:https://docs.spring.io/spring-boot/docs/1.5.7.RELEASE/reference/htmlsingle/#boot-features-logging
可以看到下面有这样一段话:Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Default configurations are provided for java Util Logging Log4j2 and Logback. In each case loggers are pre-configured to use console Output with optional file output also available.
这段话说的是springboot内部日志系统使用的是 Commons Logging 并且 SpringBoot 给 JDKLogging Log4j2(Log4j也是支持的) Logback 都提供了默认配置,并且如果使用了 Starters ,那么默认使用 Logback 。
继续往下面看,讲的是日志的五个级别,默认是INFO,然后是日志的format格式化,这些我们都略过,我们主要看26.2.1 Color-coded output这里面的。
有这样一段话If your terminal supports ANSI color output will be used to aid readability. You can set spring.output.ansi.enabled to a supported value to override the auto detection.Color coding is configured using the %clr conversion word. In its simplest form the converter will color the output according to the log level for example:
说的是如果终端支持ANSI,则使用颜色输出来帮助可读性。可以将启用的Srut.Out.Asi.Spice设置为支持的值,以重写自动检测。使用%CLR转换字来配置颜色编码。在最简单的形式中,转换器将根据日志级别对输出进行着色,例如:
The following colors and styles are supported:支持以下颜色
- blue
- cyan
- faint
- green
- magenta
- red
- yellow
文档中说了使用:输出控制台日志颜色使用%clr(){blue}来改变颜色 ()中配置打印表达式
知道了怎弄,但是我就是想知道springboot官方默认打印的日志格式(pattern)是怎么写的
我们继续看文档
在26.4 Log Levels 日志级别这里我们看到
By default Spring Boot remaps Thymeleaf INFO messages so that they are logged at DEBUG level. This helps to reduce noise in the standard log output. See LevelRemappingAppender for details of how you can apply remapping in your own configuration.
默认情况下,Spring Bug重新映射TyeleleAF信息消息,以便它们在调试级别登录。这有助于降低标准日志输出中的噪声。有关如何在自己的配置中应用重新映射的详细信息,请参见LealReMeppIppAppEnter
让我们参考LevelRemappingAppender.java这个类,我们点进来。https://github.com/spring-projects/spring-boot/blob/v1.5.7.RELEASE/spring-boot/src/main/java/org/springframework/boot/logging/logback/LevelRemappingAppender.java
看了看这个类 并没有,我们回退到logging包,然后点击loback包,看到到有一个类DefaultLogbackConfiguration.java,貌似是我们要的,我们点进来。看到如下截图,正是我们要的
官方默认日志打印的格式
private static final String CONSOLE_LOG_PATTERN = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} " "%clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} " "%clr([.15t]){faint} %clr(%-40.40logger{39}){cyan} " "%clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"; private static final String FILE_LOG_PATTERN = "%d{yyyy-MM-dd HH:mm:ss.SSS} " "${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}";
下面我就把我整理好的自定义的pattern发给大家,在springboot中配置打印一下看看
logging.pattern.console=%clr(%d{yyyy-MM-dd HH:mm:ss}){blue} %clr(-%5level){green} %clr(${PID:- }){magenta} %clr([.15thread]){red} %clr(%-40.40logger{39}){cyan} %clr(>>){faint} %clr(%msg%n){green}
怎么配置? 把这段放到你的项目的application.properties文件中就可以了,然 后启动看看效果吧,如果不满意 颜色可以自己配置的!
好了,今天的文章就到这里了,喜欢的小伙伴关注一下,给我无尽的动力吧!