美文网首页
SpringBoot 集成 logback 日志框架

SpringBoot 集成 logback 日志框架

作者: 花劫_8b1c | 来源:发表于2019-08-22 11:27 被阅读0次

    本文章仅供小编学习使用,如有侵犯他人版权,请联系小编撤回或删除

    application.properties

    logging.config=classpath:logback-spring-dev.xml
    

    logback-spring-dev.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration debug="true" scan="true" xmlns="http://ch.qos.logback/xml/ns/logback"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback
                   https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd">
        <!--
            日志类型:
                1、正常业务日志:日志文件为log-info.yyyy-MM-dd.log
                2、错误日志:log-err.yyyy-MM.log
                3、警告日志:log-warn.yyyy-MM.log
                4、请求日志文件:log-http.yyyy-MM-dd.log
                日志文件说明:日志文件以天为单位记录,单个大小为30M,一天超过30M的文件以索引0.1.2..标志,比如
                log-info.yyyy-MM-dd.0.log,log-info.yyyy-MM-dd.1.log等等
            追加器:
                infoAppender:对应info以后的所有级别,包含业务、警告、错误信息
                warnAppender:对应warn级别,包含警告信息
                errAppender:对应error级别,包含错误信息
        -->
    
        <contextName>fine-demo</contextName>
        <property name="LOG_PATH" value="logs"/>
    
    
        <!--输出到控制台-->
        <appender class="ch.qos.logback.core.ConsoleAppender" name="STDOUT">
            <encoder>
                <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} | %highlight(%-5level) | %boldRed(%thread) | %boldGreen(%logger) | %msg%n</pattern>
                <!-- 设置字符集 -->
                <charset>utf-8</charset>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>debug</level>
            </filter>
        </appender>
    
    
        <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="infoAppender">
            <!--常规日志文件-->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--归档日志文件-->
                <fileNamePattern>${LOG_PATH}/info/log-info.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>30MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
                <maxHistory>6</maxHistory>
            </rollingPolicy>
            <append>true</append>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <charset>utf-8</charset>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger Line:%-3L - %m%n</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>info</level>
            </filter>
        </appender>
        <!--警告日志文件-->
        <appender name="warnAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!--常规日志文件-->
            <file>${LOG_PATH}/log-warn.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--归档日志文件-->
                <fileNamePattern>${LOG_PATH}/warn/log-warn.%d{yyyy-MM}.%i.log</fileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>30MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
                <maxHistory>6</maxHistory>
            </rollingPolicy>
            <append>true</append>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <charset>utf-8</charset>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger Line:%-3L - %m%n</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>warn</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </appender>
        <!--错误日志文件-->
        <appender name="errAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!--常规日志文件-->
            <file>${LOG_PATH}/log-err.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--归档日志文件-->
                <fileNamePattern>${LOG_PATH}/err/log-err.%d{yyyy-MM}.%i.log</fileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>30MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
                <maxHistory>6</maxHistory>
            </rollingPolicy>
            <append>true</append>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <charset>utf-8</charset>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger Line:%-3L - %m%n</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>error</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </appender>
        <!--请求日志文件-->
        <appender name="logHttpAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOG_PATH}/log-http.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${LOG_PATH}/http/log-http.%d{yyyy-MM}.%i.log</fileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>30MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
                <maxHistory>6</maxHistory>
            </rollingPolicy>
            <append>true</append>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <charset>utf-8</charset>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger Line:%-3L - %m%n</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>info</level>
            </filter>
        </appender>
    
        <!--定义记录器-->
        <logger name="com.example.demo" level="info">
            <appender-ref ref="infoAppender"/>
            <appender-ref ref="warnAppender"/>
            <appender-ref ref="errAppender"/>
            <!--没有设置appender的话,此loger本身不打印任何信息。-->
            <!--注意level级别,从低到高all -> trace ->debug->info->warn->error->fatal->off-->
        </logger>
        <!--根日志-->
        <root level="info">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="infoAppender"/>
            <appender-ref ref="errAppender"/>
        </root>
    
    </configuration>
    

    日志详情:https://www.cnblogs.com/origalom/p/11120423.html

    相关文章

      网友评论

          本文标题:SpringBoot 集成 logback 日志框架

          本文链接:https://www.haomeiwen.com/subject/yatnsctx.html