Logback logs cannot be generated by day

Problem Description: when I checked the server log of this project recently, I found that if the project is not restarted, no new log file will be generated, and all logs will be printed to the log file of the day when the project starts to run.

Problematic profile
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--The storage address of the definition log file is not in LogBack Using relative paths in the configuration of-->
    <property name="LOG_HOME" value="/home/wbxt/wbxtpc/logs"/>
    <!-- Console output setting -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- Format output:%d Represents the date,%thread Represents the thread name,%-5level: Level 5 character width from left%msg: Log messages,%n Newline character -->
            <pattern>%d [%-10thread] %-5level %logger{80} - %msg%n</pattern>
            <!--<charset>utf8</charset>-->
        </encoder>
    </appender>

    <!-- Generate log files on a daily basis info-->
    <appender name="DAYINFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--Filename of log file output-->
            <FileNamePattern>${LOG_HOME}/hf_info.log.%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--Log file retention days-->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>info</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--Format output:%d Represents the date,%thread Represents the thread name,%-5level: Level 5 character width from left%msg: Log messages,%n Newline character-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <!--Maximum log file size-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <!-- Generate log files on a daily basis error-->
    <appender name="DAYERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--Filename of log file output-->
            <FileNamePattern>${LOG_HOME}/hf_error.log.%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--Log file retention days-->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>error</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--Format output:%d Represents the date,%thread Represents the thread name,%-5level: Level 5 character width from left%msg: Log messages,%n Newline character-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <!--Maximum log file size-->
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <!-- Log output level -->
    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="DAYERROR"/>
        <appender-ref ref="DAYINFO"/>
    </root>
</configuration>

This configuration uses the time-based rolling cutting policy TimeBasedRollingPolicy to automatically generate a new log file at 0 o'clock every day.

However, it contains a trigger policy, that is, the maximum single file is more than 10MB, which automatically generates a new log file. TimeBasedRollingPolicy is time-based and cannot be combined with other policies.

Two solutions 1. Remove triggingpolicy

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern> ${LOG_HOME}/error.log.%d{yyyy-MM-dd}.log </FileNamePattern>
        <MaxHistory>30</MaxHistory>
    </rollingPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
    </encoder>
</appender>

2. Use time and size combination policy SizeAndTimeBasedRollingPolicy

Be careful! The following% i must be added. It is the cutting sequence number after the single log file is over sized.

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <FileNamePattern> ${LOG_HOME}/hf_error.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
        <MaxHistory>30</MaxHistory>
        <maxFileSize>10MB</maxFileSize>
    </rollingPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
    </encoder>
</appender>
Correct profile

Here I'm using the second solution, the date and size strategy

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--The storage address of the definition log file is not in LogBack Using relative paths in the configuration of-->
    <property name="LOG_HOME" value="/home/wbxt/wbxtpc/logs"/>
    <!-- Console output setting -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- Format output:%d Represents the date,%thread Represents the thread name,%-5level: Level 5 character width from left%msg: Log messages,%n Newline character -->
            <pattern>%d [%-10thread] %-5level %logger{80} - %msg%n</pattern>
            <!--<charset>utf8</charset>-->
        </encoder>
    </appender>

    <!-- Generate log files on a daily basis info-->
    <appender name="DAYINFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--Filename of log file output-->
            <FileNamePattern>${LOG_HOME}/info.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
            <!--Log file retention days-->
            <MaxHistory>30</MaxHistory>
            <maxFileSize>10MB</maxFileSize>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>info</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--Format output:%d Represents the date,%thread Represents the thread name,%-5level: Level 5 character width from left%msg: Log messages,%n Newline character-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <!--Maximum log file size-->
<!--        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>-->
    </appender>

    <!-- Generate log files on a daily basis error-->
    <appender name="DAYERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--Filename of log file output-->
            <FileNamePattern>${LOG_HOME}/error.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
            <!--Log file retention days-->
            <MaxHistory>30</MaxHistory>
            <maxFileSize>10MB</maxFileSize>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>error</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--Format output:%d Represents the date,%thread Represents the thread name,%-5level: Level 5 character width from left%msg: Log messages,%n Newline character-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <!--Maximum log file size-->
<!--        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>-->
    </appender>

    <!-- Log output level -->
    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="DAYERROR"/>
        <appender-ref ref="DAYINFO"/>
    </root>
</configuration>

Tags: Programming xml encoding

Posted on Fri, 10 Jan 2020 10:46:30 -0500 by ratass2002