Log frame SLF4J+Logback

1, SLF4J(Simple Logging Facade):

   The face of the log system

2, Logback

   Specific solutions of log system

3, SLF4J+Logback

   It can be understood that SLF4J is an interface. Without the specific implementation of Logback, SLF4J cannot be used.

4, Integration with SpringBoot

  • 1. Dependency: the default log framework of spring boot is SLF4J+Logback, so the dependency of both is included in spring boot starter
  • 2. Configuration file: create the logback-spring.xml (officially recommended name) configuration file under the src\main\resources path, as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Log level from low to high TRACE < DEBUG < INFO < WARN < ERROR < FATAL,If set to WARN,Lower than WARN No output of -->
<!-- scan:When this property is set to true If the configuration document changes, it will be reloaded. The default value is true -->
<!-- scanPeriod:Set the time interval for monitoring whether the configuration document has been modified. If no time unit is given, the default unit is milliseconds.
                 When scan by true This property takes effect. The default time interval is 1 minute. -->
<!-- debug:When this property is set to true Will be printed out logback Internal log information, real-time viewing logback Operation status. The default value is false.  -->
<configuration  scan="true" scanPeriod="10 seconds">
    <contextName>logback</contextName>

    <!-- name The value of is the name of the variable, value Is the value defined by the variable. Values defined by are inserted into the logger In context. Once defined, you can“ ${}"To use variables. -->
    <property name="log.path" value="G:/logs/pmp" />

    <!--0. Log format and color rendering -->
    <!-- Color log dependent rendering class -->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    <!-- Color log format -->
    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    <!--1. Output to console-->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <!--This log appender It is used for development. Only the lowest level is configured. The log level output by the console is the log information greater than or equal to this level-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
        <encoder>
            <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
            <!-- Set character set -->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!--2. Output to document-->
    <!-- 2.1 level by DEBUG Log, time scrolling output  -->
    <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- The path and document name of the log document being recorded -->
        <file>${log.path}/web_debug.log</file>
        <!--Log document output format-->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset> <!-- Set character set -->
        </encoder>
        <!-- Rolling strategy for loggers, by date, by size -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- Log archiving -->
            <fileNamePattern>${log.path}/web-debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--Log document retention days-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- This log document records only debug Grade -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>debug</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 2.2 level by INFO Log, time scrolling output  -->
    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- The path and document name of the log document being recorded -->
        <file>${log.path}/web_info.log</file>
        <!--Log document output format-->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <!-- Rolling strategy for loggers, by date, by size -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- Daily log archive path and format -->
            <fileNamePattern>${log.path}/web-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--Log document retention days-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- This log document records only info Grade -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>info</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 2.3 level by WARN Log, time scrolling output  -->
    <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- The path and document name of the log document being recorded -->
        <file>${log.path}/web_warn.log</file>
        <!--Log document output format-->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset> <!-- Set character set here -->
        </encoder>
        <!-- Rolling strategy for loggers, by date, by size -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/web-warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--Log document retention days-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- This log document records only warn Grade -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>warn</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 2.4 level by ERROR Log, time scrolling output  -->
    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- The path and document name of the log document being recorded -->
        <file>${log.path}/web_error.log</file>
        <!--Log document output format-->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset> <!-- Set character set here -->
        </encoder>
        <!-- Rolling strategy for loggers, by date, by size -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/web-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--Log document retention days-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- This log document records only ERROR Grade -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!--
        <logger>It is used to set the log printing level of a package or a specific class
        And specify<appender>. <logger>There is only one. name Attribute,
        An optional level And an optional addtivity Attribute.
        name:Used to specify the logger A package of constraints or a specific class.
        level:Used to set the print level, case independent: TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF,
              There is also a special value INHERITED Or synonyms NULL,Represents the level of enforcement authority.
              If this property is not set, the current logger Will inherit the level of the superior.
        addtivity:To superior or not logger Transfer printing information. The default is true. 
        <logger name="org.springframework.web" level="info"/>
        <logger name="org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor" level="INFO"/>
    -->

    <!--
        Use mybatis At that time, sql Statement is debug Next, we will print, and here we only configure info,So I want to see sql Statement, there are two operations:
        The first kind of handle.<root level="info">Change to<root level="DEBUG">This will print sql,But there will be a lot of other news in the log
        The second is to give it alone dao Directory configuration debug Mode, the code is as follows, configure as follows sql Statement will print, others are normal info Level:
        [logging.level.org.mybatis=debug logging.level.dao=debug]
     -->

    <!--
        root Node is a required node. It is used to specify the most basic log output level. There is only one node level attribute
        level:Used to set the print level, case independent: TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF,
        Cannot be set to INHERITED Or synonyms NULL. The default is DEBUG
        It can contain zero or more elements to identify this appender Will be added to this logger. 
    -->

    <!-- 4. Final strategy -->
    <!-- 4.1 development environment:Print Console -->
    <springProfile name="dev">
        <logger name="com.sdcm.pmp" level="debug"/>
    </springProfile>

    <root level="info">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="DEBUG_FILE" />
        <appender-ref ref="INFO_FILE" />
        <appender-ref ref="WARN_FILE" />
        <appender-ref ref="ERROR_FILE" />
    </root>

    <!-- 4.2 production environment:Output to document
    <springProfile name="pro">
        <root level="info">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="DEBUG_FILE" />
            <appender-ref ref="INFO_FILE" />
            <appender-ref ref="ERROR_FILE" />
            <appender-ref ref="WARN_FILE" />
        </root>
    </springProfile> -->

</configuration>
114 original articles published, praised 31, visited 70000+
Private letter follow

Tags: SQL Spring Attribute xml

Posted on Mon, 13 Jan 2020 10:25:21 -0500 by LeeRoy8888