Custom log configuration logback-spring.xml in SpringBook

Keywords: xml Spring log4j SQL

scene

Log control and management can still be well supported through system attributes and traditional SpringBook external configuration files.

 

Naming Specifications for Different Logging Systems

Logback:logback-spring.xml  logbak-spring.proovy  logback.xml  logback.groovy

Log4j:log4j-spring.properties  log4j-spring.xml  log4j.properties  log4j.xml

Log4j2:log4j2-spring.xml  log4j2.xml

JDK(Java Util Logging):logging.properties

 

SpringBoot officially recommends using the file name with - spring as the log configuration file.

If you do not want to use the specified configuration file name, you can specify in the configuration file:

logging.config=classpath:logging-config.xml

Realization

New File--logback-spring.xml under project src/main/resource

After completion

 

Configuration file content:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Log levels range from low to high TRACE < DEBUG < INFO < WARN < ERROR < FATAL,If set to WARN,Lower than WARN None of the information will be exported. -->
<!-- scan:When this property is set to true When the configuration document changes, it will be reloaded with the default value true -->
<!-- scanPeriod:Set the time interval for monitoring whether the configuration document is modified or not. If no time unit is given, the default unit is milliseconds.
                 When scan by true When this property is in effect. The default time interval is 1 minute. -->
<!-- debug:When this property is set to true When printed out logback Internal log information for real-time viewing logback Operation status. The default value is false.  -->
<configuration  scan="true" scanPeriod="10 seconds">
    <contextName>logback</contextName>

    <!-- name The value is the name of the variable. value The value of a variable is the value defined by the variable. The defined values are inserted into the logger Context. After definition, you can make it possible to“ ${}"To use variables. -->
    <property name="log.path" value="F:/logs" />

    <!--0. Log format and color rendering -->
    <!-- Color Log Dependent Rendering Classes -->
    <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" />
    <!-- Colour 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 For development purposes, only the lowest level is configured, and the log level output by the console is greater than or equal to this level of log information.-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
        <encoder>
            <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
            <!-- Setting Character Set -->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!--2. Output to Document-->
    <!-- 2.1 level by DEBUG Log, time scroll output  -->
    <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- The path and document name of the log document being logged -->
        <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> <!-- Setting Character Set -->
        </encoder>
        <!-- Logger rolling strategy, by date, by size -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- Archive -->
            <fileNamePattern>${log.path}/web-debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--Days of retention of log documents-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- This log document is recorded 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 scroll output  -->
    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- The path and document name of the log document being logged -->
        <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>
        <!-- Logger rolling strategy, by date, by size -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- Daily log archiving 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>
            <!--Days of retention of log documents-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- This log document is recorded 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 scroll output  -->
    <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- The path and document name of the log document being logged -->
        <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 the character set here -->
        </encoder>
        <!-- Logger rolling strategy, 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>
            <!--Days of retention of log documents-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- This log document is recorded 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 scroll output  -->
    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- The path and document name of the log document being logged -->
        <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 the character set here -->
        </encoder>
        <!-- Logger rolling strategy, 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>
            <!--Days of retention of log documents-->
            <maxHistory>15</maxHistory>
        </rollingPolicy>
        <!-- This log document is recorded only ERROR Grade -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!--
        <logger>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 one addtivity Attribute.
        name:Used to specify acceptance logger Constrained a package or a specific class.
        level:Used to set the printing level, case-independent: TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF,
              There's another special value. INHERITED Or synonyms NULL,Represents the level at which superiors are enforced.
              If this property is not set, then the current logger Will inherit the superior level.
        addtivity:Whether to go up to superiors or not logger Transfer print 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 Print next, and here we only configure it. info,So I want to check it out. sql There are two operations for statements:
        The first kind of handle.<root level="info">Change to<root level="DEBUG">This will print. sql,But then there will be a lot of other news in the log.
        The second is to give it alone. dao Subdirectory Configuration debug Patterns, coded as follows, are configured as follows sql Statements will print, others are normal info Level:
        [logging.level.org.mybatis=debug logging.level.dao=debug]
     -->

    <!--
        root Nodes are optional nodes that specify the most basic level of log output. There is only one node. level attribute
        level:Used to set the printing 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, identifying 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>

Operating project, effect:

Open the configuration file path

Source download:

https://download.csdn.net/download/badao_liumang_qizhi/11062063

Posted by vadercole on Wed, 27 Mar 2019 20:24:29 -0700