Home > Java > javaTutorial > What is the method for color log printing of Linux deployment springboot project?

What is the method for color log printing of Linux deployment springboot project?

王林
Release: 2023-05-13 15:34:06
forward
1277 people have browsed it

Linux部署springboot项目彩色日志打印

未做处理tail日志log.out文件

tail -f log.out
Copy after login

原色效果图

What is the method for color log printing of Linux deployment springboot project?

tail日志log.out文件

tail -f log.out | perl -pe's/(INFO)|(DEBUG)|(WARN)|(ERROR)|(^[0-9-:.\s]{10,23})|((?<p>彩色效果图</p><p><img src="https://img.php.cn/upload/article/000/465/014/168396324927518.png" alt="What is the method for color log printing of Linux deployment springboot project?"></p><p>注意:</p><p>通过正则表达式和颜色来对应分配</p><h3>springboot配置控制台彩色日志输出</h3><p>最新补充:</p><p>如果使用的是application.properties进行配置,则需要配置下面项目:</p><pre class="brush:java;">spring.output.ansi.enabled=always  # 需要将其设置为always
Copy after login

Spring默认使用Logback来进行日志输出,同时,控制台不同参数输出为不同的颜色,看起来逼格很高。

但是,当我们使用自己默认的配置的时候,彩色消失了。我想,在源码中,肯定有默认的配置,所以就去源码中找了一圈,很快就找到了,这里和大家分享下。

默认的配置位置:(SpringBoot 2.1.0 版本)

org.springframework.boot.logging.logback 包下 defaults.xml 文件中,配置了相关属性。
 
<included>
	<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{${LOG_DATEFORMAT_PATTERN:-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}}"/>
	<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
 
	<logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
	<logger name="org.apache.catalina.util.LifecycleBase" level="ERROR"/>
	<logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"/>
	<logger name="org.apache.sshd.common.util.SecurityUtils" level="WARN"/>
	<logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/>
	<logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="ERROR"/>
	<logger name="org.hibernate.validator.internal.util.Version" level="WARN"/>
</included>
Copy after login

我们可以看到,CONSOLE_LOG_PATTERN 参数就是默认的 输出格式配置。我们可以通过在application.properties文件中修改属性logging.pattern.console属性,来定制自己需要的彩色输出。

这里,分享一下我自己修改的日志输出格式:

logging.pattern.console=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(---){faint} %clr(%-80.80logger{79}){cyan} %clr(:){faint} %m%n
Copy after login

去掉了 线程名 节点,调整了类名的长度限制。

如果希望使用自定义的logback.xml,则需要加上 相应的转换器类:

<substitutionProperty name="log.pattern" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(---){faint} %clr(%-80.80logger{79}){cyan} %clr(:){faint} %m%n%wEx"/>
 
<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"/>
 
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <pattern>${log.pattern}</pattern>
    </encoder>
</appender>
Copy after login

效果展示:

What is the method for color log printing of Linux deployment springboot project?

The above is the detailed content of What is the method for color log printing of Linux deployment springboot project?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template