Logback日志配置文件添加颜色区分

2022-06-09 19:30:13 浏览数 (1)

Logback日志配置文件添加颜色区分

添加颜色

<pattern>%yellow(%d{yyyy-MM-dd HH:mm:ss.SSS}) %blue([%thread]) %highlight(%-5level) %green(%logger) - %msg%n</pattern>

logback.xml

代码语言:javascript复制
<configuration>
    <!--为了防止进程退出时,内存中的数据丢失,请加上此选项-->
    <shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%yellow(%d{yyyy-MM-dd HH:mm:ss.SSS}) %blue([%thread]) %highlight(%-5level) %green(%logger) - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 可用来获取StatusManager中的状态 -->
    <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/>

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

    <!-- 开发环境 -->
    <springProfile name="default">
        <logger name="com.xxx.*" level="info" additivity="false">
            <appender-ref ref="console"/>
        </logger>
        <logger name="org.springframework.*" level="info" additivity="false">
            <appender-ref ref="console"/>
        </logger>
    </springProfile>
    <!--测试环境-->
    <springProfile name="test">
        <logger name="com.xxx" level="info" additivity="false">
            <appender-ref ref="console"/>
        </logger>
    </springProfile>
    <!-- 生产环境 -->
    <springProfile name="prod">
        <logger name="com.xxx" level="error" additivity="false">
            <appender-ref ref="console"/>
        </logger>
    </springProfile>

    <logger name="com.xxx.dao" level="debug" additivity="false">
        <appender-ref ref="console"/>
    </logger>

</configuration>

0 人点赞