logback 配置

感情迁移 提交于 2020-01-19 15:44:44

根据spring_actives_profile=xxx生效


 

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds">
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <springProperty scope="context" name="appName" source="spring.application.name"/>

    <!-- 定义参数 -->
    <property name="log.file" value="/data/logs/${appName}"></property>
    <property name="log.maxHistory" value="365"/>
    <property name="log.pattern"
              value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n"/>

    <!-- 控制台设置 -->
    <appender name="consoleAppender"
              class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
    </appender>

    <appender name="consoleAppenderSync"
              class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="consoleAppender"/>
        <queueSize>1024</queueSize>
        <discardingThreshold>0</discardingThreshold>
    </appender>

    <!-- DEBUG -->
    <appender name="debugAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 文件路径 -->
        <file>${log.file}/debug.log</file>
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 文件名称 -->
            <fileNamePattern>${log.file}/debug/debug.%d{yyyy-MM-dd}.log.gz
            </fileNamePattern>
            <!-- 文件最大保存历史数量 -->
            <MaxHistory>${log.maxHistory}</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="debugAppenderSync"
              class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="debugAppender"/>
        <queueSize>1024</queueSize>
        <discardingThreshold>0</discardingThreshold>
    </appender>

    <!-- INFO -->
    <appender name="infoAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 文件路径 -->
        <file>${log.file}/info.log</file>
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 文件名称 -->
            <fileNamePattern>${log.file}/info/info.%d{yyyy-MM-dd}.log.gz
            </fileNamePattern>
            <!-- 文件最大保存历史数量 -->
            <MaxHistory>${log.maxHistory}</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="infoAppenderSync"
              class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="infoAppender"/>
        <queueSize>1024</queueSize>
        <discardingThreshold>0</discardingThreshold>
    </appender>

    <!-- ERROR -->
    <appender name="errorAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 文件路径 -->
        <file>${log.file}/error.log</file>
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 文件名称 -->
            <fileNamePattern>${log.file}/error/error.%d{yyyy-MM-dd}.log.gz
            </fileNamePattern>
            <!-- 文件最大保存历史数量 -->
            <MaxHistory>${log.maxHistory}</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="errorAppenderSync"
              class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="errorAppender"/>
        <queueSize>1024</queueSize>
        <discardingThreshold>0</discardingThreshold>
    </appender>


    <logger name="com.alibaba.nacos" level="error"/>
    <logger name="org.apache.http.impl.conn" level="error"/>

    <!--默认配置spring.profiles.active,生产环境-->
    <springProfile name="prod">

        <logger name="com.test" level="info">
            <appender-ref ref="debugAppenderSync"/>
            <appender-ref ref="infoAppenderSync"/>
            <appender-ref ref="errorAppenderSync"/>
        </logger>

        <root level="info">
            <appender-ref ref="consoleAppenderSync"/>
        </root>
    </springProfile>

    <!--默认配置不spring.profiles.active,和生产环境保持一致-->
    <springProfile name="!prod">
        <logger name="org.springframework" level="info"/>
        <logger name="com.test" level="info">
            <appender-ref ref="debugAppenderSync"/>
            <appender-ref ref="infoAppenderSync"/>
            <appender-ref ref="errorAppenderSync"/>
        </logger>

        <root level="info">
            <appender-ref ref="consoleAppenderSync"/>
        </root>
    </springProfile>

    <!--测试和开发日志一致-->
    <springProfile name="dev,test">
        <logger name="org.mybatis" level="debug"/>
        <logger name="org.springframework" level="debug"/>

        <logger name="com.test" level="debug">
            <appender-ref ref="debugAppenderSync"/>
            <appender-ref ref="infoAppenderSync"/>
            <appender-ref ref="errorAppenderSync"/>
        </logger>

        <root>
            <appender-ref ref="consoleAppenderSync"/>
        </root>
    </springProfile>


</configuration>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!