spring boot slf4j + logback

引用自: https://miaoxinguo.github.io/logback/2015/02/05/logback.setting.html
其他参考: https://juejin.cn/post/6844903790349385735
输出格式配置可以参考官方文档: http://logback.qos.ch/manual/layouts.html
配置文件放在项目的src/main/resource下,以logback-spring.xml命名,可以分环境打包,具体见第2种
<?xml versoin="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <!-- 上下文属性 -->
    <property name="LOG_BASE" value="/home/log"></property>

    <!-- 系统日志按时间段输出到文件 -->
    <appender name="infoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logbak.classic.filter.ThresholdFilter"> <!-- 过滤低于设置级别的日志 -->
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_BASE}/info.%d{yyyy-MM-dd}.log</FileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 错误日志按时间段输出到文件 -->
    <appender name="errorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logbak.classic.filter.ThresholdFilter"> <!-- 过滤低于设置级别的日志 -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_BASE}/error.%d{yyyy-MM-dd}.log</FileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 控制第三方包的日志 -->
    <logger name="org.springframework" level="WARN" />

    <root level="INFO">
        <appender-ref ref="infoLog" />
        <appender-ref ref="errorLog" />
    </root>
</configuration>
2.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">

    <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径 -->
    <property name="LOG_HOME" value="/app/pttms/logs"/>

    <!--
        开发环境
    -->
    <springProfile name="dev">

        <!-- 控制台输出 -->
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%mdc封装版本号参数, %n是换行符 -->
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg - %mdc%n</pattern>
            </encoder>
        </appender>

        <!-- logger configure -->
        <logger name="org.springframework" level="WARN"/>
        <logger name="org.springframework.boot.context.embedded" level="INFO"/>
        <logger name="org.apache" level="WARN"/>
        <logger name="org.quartz" level="WARN"/>
        <logger name="com.mchange" level="WARN"/>
        <logger name="org.hibernate.validator" level="WARN"/>
        <logger name="net.sf.ehcache" level="WARN"/>

        <!-- 日志输出级别 -->
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
        </root>

    </springProfile>

    <!--
        测试、生产环境
    -->
    <springProfile name="!dev">

        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%mdc封装版本号参数, %n是换行符 -->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg - %mdc%n</pattern>
            </encoder>
        </appender>

        <!-- 按照每天生成日志文件 -->
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>pttms.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--日志文件输出的文件名 -->
                <FileNamePattern>${LOG_HOME}/../pttms.%d{yyyy-MM-dd}.%i.log</FileNamePattern>

                <!--日志文件数量 -->
                <MaxHistory>1</MaxHistory>

                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <!--日志文件最大 100MB -->
                    <maxFileSize>100MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>

            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%mdc封装版本号参数, %n是换行符 -->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg - %mdc%n</pattern>
            </encoder>
        </appender>

        <!-- 按照每天生成日志文件 INFO级别-->
        <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--日志文件输出的文件名 -->
                <FileNamePattern>${LOG_HOME}/pttms.info.%d{yyyy-MM-dd}.log</FileNamePattern>

                <!--日志文件保留天数 -->
                <MaxHistory>30</MaxHistory>
            </rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%mdc封装版本号参数, %n是换行符 -->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg - %mdc%n</pattern>
            </encoder>
        </appender>

        <!-- 按照每天生成日志文件 ERROR级别-->
        <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>ERROR</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--日志文件输出的文件名 -->
                <FileNamePattern>${LOG_HOME}/pttms.error.%d{yyyy-MM-dd}.log</FileNamePattern>

                <!--日志文件保留天数 -->
                <MaxHistory>30</MaxHistory>
            </rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%mdc封装版本号参数, %n是换行符 -->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg - %mdc%n</pattern>
            </encoder>
        </appender>

        <!-- 队列日志单独文件 -->
        <appender name="mqLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
                <evaluator>
                    <matcher>
                        <Name>mqMatcher</Name>
                        <regex>.*队列日志.*</regex>
                    </matcher>
                    <expression>mqMatcher.matches(formattedMessage)</expression>
                </evaluator>
                <OnMatch>ACCEPT</OnMatch>
                <OnMismatch>DENY</OnMismatch>
            </filter>

            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--日志文件输出的文件名 -->
                <FileNamePattern>${LOG_HOME}/pttms.mq.%d{yyyy-MM-dd}.%i.log</FileNamePattern>

                <!--日志文件数量 -->
                <MaxHistory>30</MaxHistory>

                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <!--日志文件最大 100MB -->
                    <maxFileSize>100MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>

            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%mdc封装版本号参数, %n是换行符 -->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            </encoder>
        </appender>

        <!-- logger configure -->
        <logger name="org.springframework" level="WARN"/>
        <logger name="org.springframework.boot.context.embedded" level="INFO"/>
        <logger name="org.apache" level="WARN"/>
        <logger name="org.quartz" level="WARN"/>
        <logger name="org.hibernate.validator" level="WARN"/>
        <logger name="net.sf.ehcache" level="WARN"/>

        <!-- 日志输出级别 -->
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="INFO"/>
            <appender-ref ref="ERROR"/>
            <appender-ref ref="FILE"/>
            <appender-ref ref="mqLog"/>
        </root>
    </springProfile>

</configuration>
(0)

相关推荐