asedTriggeringPolicy触发策略不适用于Logb

asedTriggeringPolicy触发策略不适用于Logb

本文介绍了FixedWindowRollingPolicy和SizeBasedTriggeringPolicy触发策略不适用于Logback 1.1.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的登录配置,即使超过2MB(maxfileSize)也没有归档文件.我使用的是1.1.7版本的登录.不确定到底是什么问题.

here is my logback configuration, files are not getting archived even after exceeidng 2MB(maxfileSize).I am using 1.1.7 version of logback.Not sure what exactly is the problem.

<configuration>
    <appender name="RFILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>../logs/esync.log</file>
        <encoder>
            <pattern>%d{MM-dd-yy HH:mm:ss} %-5level %mdc %logger{60}::%M:%line -
                %msg%n
            </pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>../logs/archive/esync.%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>


            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>2MB</maxFileSize>
</triggeringPolicy>

    </appender>

    <root level="${log.level:-INFO}">
        <appender-ref ref="RFILE" />
    </root>
</configuration>

推荐答案

如果您仍然遇到此问题,那么我在这里收集了loback网站的解决方案: http://jira.qos.ch/browse/LOGBACK-74 鉴于在某些平台上计算文件大小是一项相对昂贵的操作,因此在SizeBasedTriggeringPolicy类的isTriggeringEvent方法中,我们每16次调用仅检查一次文件大小.对于寿命长的应用程序来说,这不是问题,因为它最终将进行16次调用对于isTriggeringEvent().对于短寿命的应用程序,在关闭前很少进行日志记录调用,isTriggeringEvent()可能永远不会被调用16次.

If you are still facing this issue, here I gathered the solution from loback site : http://jira.qos.ch/browse/LOGBACK-74"Given that on some platforms computing file size is a relatively expensive operation, in the isTriggeringEvent method of SizeBasedTriggeringPolicy class we check for the file size only once every 16 calls. For long lived applications this is not a problem since it will eventually make 16 calls to isTriggeringEvent(). However, for short lived applications making few logging calls before shutting down, isTriggeringEvent () may never be called 16 times"

在这些行上,为了测试寿命短的应用程序,我将线程设置为休眠状态,这导致日志文件以设置的大小滚动.记录语句时我的代码:

On these lines, to test my short lived application I set my thread to sleep for a while, this lead the log file to rolled-over with the set size.My Code while logging the statement :

public class MyLoggingClass {

    private static final Logger logger = LoggerFactory.getLogger(MyLoggingClass.class);

    public void method1() {

        for(int i=0; i<50;i++) {
            logger.debug("This is Debug Message : " + i);
            logger.info("This is Info Message : " + i);
            try{
                Thread.sleep(1000);
            }catch(Exception e){}
        }
    }
}

这将完全按照设置的最大大小(即我的情况为1KB)创建日志文件,如果大小超出限制,则将其翻转.我希望这也能解决您的问题.

This created log files exactly with the set max size(i.e. 1KB in my case) and rolled over if the size limit exceed. I hope this does fix your problem too.

这篇关于FixedWindowRollingPolicy和SizeBasedTriggeringPolicy触发策略不适用于Logback 1.1.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 00:48