1

嗨,有什么方法可以在应用程序启动时设置滚动日志,或者如果是新的一天,并通过配置文件将历史记录保存 30 天或 1 个月(不是 30 个文件)。我找了几个小时,但一无所获。可能这种方式不存在,需要编写。

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d [%thread] %-5level %logger{50} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/logs/server.log</file>
        <encoder>
            <pattern>%d [%thread] %-5level %logger{50} - %msg%n
            </pattern>
        </encoder>

        <!-- Join this time based rolling with-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/logs/server.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <!-- Mean 30 days not 30 files -->
            <maxHistory>30</maxHistory> 
        </rollingPolicy>

        <!-- New start baser Rolling-->
        ...
    </appender>

    <root level="INFO">
    <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

</configuration>
4

1 回答 1

2

这个问题由https://stackoverflow.com/a/30801658/844648复制和回答

我终于弄明白了。我可以按大小、时间和启动滚动。这是解决方案:

第一次创建你自己的班级

@NoAutoStart
public class StartupSizeTimeBasedTriggeringPolicy<E> extends SizeAndTimeBasedFNATP<E> {

    private boolean started = false;

    @Override
    public boolean isTriggeringEvent( File activeFile, E event ) {
        if ( !started ) {
            nextCheck = 0L;
            return started = true;
        }

        return super.isTriggeringEvent( activeFile, event );
    };
}

2、配置lockbax

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOGS_DIR}/${FILE_NAME}.log</file>
    <encoder>
        <pattern>%d [%thread] %-5level %logger{50} - %msg%n</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOGS_DIR}/${FILE_NAME}.%d{yyyy-MM-dd}_%d{HHmmss,aux}.%i.log.zip</fileNamePattern>
        <maxHistory>30</maxHistory>
        <TimeBasedFileNamingAndTriggeringPolicy class="my.StartupSizeTimeBasedTriggeringPolicy">
            <MaxFileSize>250MB</MaxFileSize> 
        </TimeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>
于 2015-06-12T11:23:13.030 回答