当前位置:网站首页>logback. XML configure logs of different levels and set color output

logback. XML configure logs of different levels and set color output

2022-07-07 16:24:00 kill-java

yml perhaps properties The configuration file

logging.config=classpath:logback.xml

resources New under the directory logback.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!-- Root tag -->
<configuration>
    <!-- Set a variable ,name Variable name ,value Value , have access to ${
     Variable name } Way to use -->
    <property name="DIR" value="log"/>
    <property name="LOG_HOME" value="/F:/project/logs"/>
    <property name="NAME" value="log"/>
    <property name="MDC_LOG_PATTERN"
              value="%red(%d{yyyy-MM-dd'T'HH:mm:ss.SSS}) %green(%p filesystem %t) %blue(%logger{50}) %yellow([line:%L %msg]%n)"></property>

    <!--  Run loggers , Date roll record  -->
    <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--  The path and filename of the log file being logged  -->
        <file>${
    LOG_HOME}/${
    NAME}/info/${
    NAME}.log</file>
        <!--  Rolling policy for loggers , By date , Record by size -->

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            <fileNamePattern>${
    LOG_HOME}/${
    NAME}/info/${
    NAME}-%d{
    yyyy-MM-dd}.%i.log</fileNamePattern>
            <!--  In addition to logging , It is also configured that the log file cannot exceed 50M, If more than 50M, Log files will be indexed 0 Start ,   Name the log file , for example bizlog-biz-20181219.0.log -->
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>50MB</maxFileSize>
                <!-- Storage time 3 God -->
                <!--<MaxHistory>3</MaxHistory>-->
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <!--  Add method to record log  -->
        <append>true</append>
        <!--  Format of log file  -->
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{
    yyyy/MM/dd' 'HH:mm:ss.SSS} %X{
    req.requestId}[line:%L %msg] %n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <!--  This log file only records info Grade  -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>info</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--  The path and filename of the log file being logged  -->
        <file>${
    LOG_HOME}/${
    NAME}/error/${
    NAME}.log</file>
        <!--  Rolling policy for loggers , By date , Record by size -->

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            <fileNamePattern>${
    LOG_HOME}/${
    NAME}/error/${
    NAME}-%d{
    yyyy-MM-dd}.%i.log</fileNamePattern>
            <!--  In addition to logging , It is also configured that the log file cannot exceed 50M, If more than 50M, Log files will be indexed 0 Start ,   Name the log file , for example bizlog-biz-20181219.0.log -->
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>50MB</maxFileSize>
                <!-- Storage time 3 God -->
                <!--<MaxHistory>3</MaxHistory>-->
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <!--  Add method to record log  -->
        <append>true</append>
        <!--  Format of log file  -->
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{
    yyyy/MM/dd' 'HH:mm:ss.SSS} %X{
    req.requestId}[line:%L %msg] %n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <!--  This log file only records info Grade  -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>error</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <!--ConsoleAppender Is printed to the console -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!--encoder  Default configuration is PatternLayoutEncoder-->
        <encoder>
            <pattern>${
    MDC_LOG_PATTERN}</pattern>
            <charset>utf-8</charset>
        </encoder>
        <!-- This log appender It's for development , Configure only the lowest level , The console output log level is greater than or equal to this level of log information -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>all</level>
        </filter>
    </appender>

    <!-- root loger. only one level attribute , It should have been named "root".-->
    <root level="info">
        <appender-ref ref="info"/>
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="error"/>
    </root>

</configuration>
原网站

版权声明
本文为[kill-java]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071411598250.html