Logback.xml 代码高亮、显示行号 的配置

祖传配置辣

借助 SpringBoot 的配置


效果如图

image

<configuration>
    <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>

    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>

    <property name="CONSOLE_LOG_PATTERN"
              value="%clr(%date){faint} %clr(%-5level){magenta} [%thread] %clr(%logger{10}){cyan}\(%F:%L\\) %highlight(%msg) %n"/>
    
    <property name="CONSOLE_LOG_PATTERN_NO_COLOR"
              value="%date %-5level [%thread] %logger{10} %msg %n"/>

    <appender name="STDOUT_COLORFUL" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT_COLORFUL"/>
    </root>
</configuration>

Logback 1.2.11 + Slf4j 1.7.32 原生配置


效果如图

image

配置如下

<configuration>
    <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>

    <property name="CONSOLE_LOG_PATTERN"
              value="%date{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) [%boldYellow(%thread)] %boldGreen(%logger{10})\(%F:%L\\) %msg%n"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

Logback 1.4.4 + Slf4j 2.0.3 配置

效果如下
image

配置如下, 顺带加上了 xsd 文件, idea 识别后可以自动补全

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns="http://ch.qos.logback/xml/ns/logback"
               xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback
                https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd">
    <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>

    <property name="CONSOLE_LOG_PATTERN"
              value="%date{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) [%boldYellow(%thread)] %boldGreen(%logger{10})(%F:%L\) %msg%n"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
    </root>

</configuration>

综合版

包含 1.2.x 和 1.4.x 版的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns="http://ch.qos.logback/xml/ns/logback"
               xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback
                https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd">
    <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>

    <!--  1.2.x 版本的 logback 配置  -->
    <property name="CONSOLE_LOG_PATTERN_V1.2"
              value="%date{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) [%boldYellow(%thread)] %boldGreen(%logger{10})\(%F:%L\\) %msg%n"/>
    <!--  1.4.x 版本的 logback 配置  -->
    <property name="CONSOLE_LOG_PATTERN_V1.4"
              value="%date{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) [%boldYellow(%thread)] %boldGreen(%logger{10})(%F:%L\) %msg%n"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${CONSOLE_LOG_PATTERN_V1.4}</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
    </root>

    <logger name="com.mrx" level="debug"/>

</configuration>

包含日志文件输出的配置文件

这里的 [%X{traceId}] 是 MDC 的 key, 具体怎么用就不仔细展开了

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns="http://ch.qos.logback/xml/ns/logback"
               xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback
                https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd">
    <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>

    <!--  1.2.x 版本的 logback 配置  -->
    <property name="CONSOLE_LOG_PATTERN_V1.2"
              value="%date{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) [%boldYellow(%thread)] %boldGreen(%logger{10})\(%F:%L\\) %msg%n"/>
    <!--  1.4.x 版本的 logback 配置  -->
    <property name="CONSOLE_LOG_PATTERN_V1.4"
              value="%date{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) [%boldYellow(%thread)] [%X{traceId}] %boldGreen(%logger{10})(%F:%L\) %msg%n"/>

    <property name="FILE_LOG_PATTERN_V1.4"
              value="%date{yyyy-MM-dd HH:mm:ss} %-5level [%thread] [%X{traceId}] %logger{10}(%F:%L\) %msg%n"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${CONSOLE_LOG_PATTERN_V1.4}</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/logFile.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs/logFile-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN_V1.4}</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>

    <logger name="com.mrx" level="debug"/>

</configuration>
posted @ 2022-07-01 17:57  博麗靈夢  阅读(1143)  评论(0编辑  收藏  举报