欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

logback

程序员文章站 2022-07-03 15:54:54
...

 

 

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
    <property name="LOG_HOME" value="${LOG_PATH}"/>

    <property name="fileLayoutPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %p %c:%L - %msg%n" />

    <!-- 控制台输出 -->
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <Encoding>UTF-8</Encoding>
        <encoder>
            <pattern>${fileLayoutPattern}</pattern>
        </encoder>
    </appender>

    <!-- 按照每天生成日志文件 -->
    <appender name="normalAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Encoding>UTF-8</Encoding>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <FileNamePattern>${LOG_HOME}/cc-%d{yyyyMMdd}.log</FileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %p %c:%L - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Encoding>UTF-8</Encoding>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/cc-error-%d{yyyyMMdd}.log</FileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>${fileLayoutPattern}</pattern>
        </encoder>

        <!-- 只打印错误日志 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 某个logger的additivity设置为false,表示log只打印到本log的appender中,而不再打印到其父logger的appender。 -->
    <logger name="org.apache.zookeeper.ClientCnxn" level="info" additivity="false"/>

    <!-- 开发环境 -->
    <springProfile name="dev">
        <root level="debug">
            <appender-ref ref="stdout"/>
        </root>
    </springProfile>


    <springProfile name="test">
        <root level="debug">
            <appender-ref ref="normalAppender"/>
            <appender-ref ref="errorAppender"/>
        </root>
    </springProfile>


    <springProfile name="prod">
        <root level="info">
            <appender-ref ref="normalAppender"/>
            <appender-ref ref="errorAppender"/>
        </root>
    </springProfile>

</configuration>

 

 

转载于:https://my.oschina.net/anxiaole/blog/1843463