[logback-user] How to get rollingfileappender to roll over based on log size

Jeremy Kane jkane001 at gmail.com
Tue Jul 8 13:47:56 CEST 2014


That did the trick,  thanks!
On May 27, 2014 2:09 AM, "Tony Trinh" <tony19 at gmail.com> wrote:

> Hello Jeremy,
>
> You mention a size-based policy, but I only see a time-based policy in
> your config.
>
> The following patterns should work for RollingFileAppender. The code
> snippets are based on the code you pasted, and I did not test them. Please
> let me know if they work for you.
>
> -Tony
>
> *Size-and-time-based rollover [1]*
>
>    - rolling policy = new TimeBasedRollingPolicy()
>    - TimeBasedRollingPolicy.timeBasedFileNamingAndTriggeringPolicy = new
>       SizeAndTimeBasedFNATP()
>    - trigger policy = do not set (already handled by
>    TimeBasedRollingPolicy)
>
> Code:
>
>     TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new
> TimeBasedRollingPolicy<ILoggingEvent>();
>     rollingPolicy.setMaxHistory(2);
>     rollingPolicy.setFileNamePattern(fileDirectory + "/log.%d.txt");
>     rollingPolicy.setParent(rollingFileAppender);
>     rollingPolicy.setContext(loggerContext);
>
>     // size-and-time-based rollover (5MB files).
>     // We don't need to set this triggering policy's context or start
>     // it because RollingFileAppender.start() does all that for us.
>     // SizeAndTimeBasedFNATP.start() is relatively expensive, so do it
>     // only once if we can avoid it.
>     SizeAndTimeBasedFNATP<ILoggingEvent> triggerPolicy = new
> SizeAndTimeBasedFNATP<ILoggingEvent>();
>     triggerPolicy.setMaxFileSize("5MB");
>     rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggerPolicy);
>
>     rollingPolicy.start();
>
> *Size-based rollover [2]*
>
>    - rolling policy = new FixedWindowRollingPolicy()
>    - trigger policy = new SizeBasedTriggeringPolicy()
>
> Code:
>
>     // size-based rollover (fixed window of 3 files, 5MB each)
>     FixedWindowRollingPolicy rollingPolicy2 = new
> FixedWindowRollingPolicy();
>     rollingPolicy2.setContext(loggerContext);
>     rollingPolicy2.setFileNamePattern("foo.%i.log");
>     rollingPolicy2.setMinIndex(1);
>     rollingPolicy2.setMaxIndex(3);
>     rollingPolicy2.start();
>
>     // size-based rollover needs a triggering policy
>     SizeBasedTriggeringPolicy<ILoggingEvent> triggerPolicy2 = new
> SizeBasedTriggeringPolicy<ILoggingEvent>();
>     triggerPolicy2.setContext(loggerContext);
>     triggerPolicy2.setMaxFileSize("5MB");
>     triggerPolicy2.start();
>
>
> [1] http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedFNATP
> [2] http://logback.qos.ch/manual/appenders.html#SizeBasedTriggeringPolicy
>
>
>
> On Wed, May 14, 2014 at 8:53 PM, Jeremy Kane <jkane001 at gmail.com> wrote:
>
>> I've been trying to get the rollinglogfileappender to roll-over at a
>> certain file size, but I've had no luck.  I can get it to roll over at
>> midnight each night, but I still would prefer to either combine them or
>> have it just be file-size limited.
>>
>> My config is such:
>>
>> // Rolling Log Appender
>> String fileDirectory = KanetikApplication.getFileDirectory(context);
>>
>> RollingFileAppender<ILoggingEvent> rollingFileAppender = new
>> RollingFileAppender<ILoggingEvent>();
>> rollingFileAppender.setContext(loggerContext);
>>  rollingFileAppender.setAppend(true);
>> rollingFileAppender.setFile(fileDirectory + "/log.txt");
>>
>> TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new
>> TimeBasedRollingPolicy<ILoggingEvent>();
>> rollingPolicy.setMaxHistory(2);
>>  rollingPolicy.setFileNamePattern(fileDirectory + "/log.%d.txt");
>> rollingPolicy.setParent(rollingFileAppender);
>>  rollingPolicy.setContext(loggerContext);
>>  rollingPolicy.start();
>>
>> rollingFileAppender.setRollingPolicy(rollingPolicy);
>>
>> PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
>>  fileEncoder.setContext(loggerContext);
>> fileEncoder.setPattern("%d{h:mm:ss} %level - %msg%n");
>>  fileEncoder.start();
>>
>> rollingFileAppender.setEncoder(fileEncoder);
>> rollingFileAppender.start();
>>
>> // LogCat Appender
>> PatternLayoutEncoder logcatEncoder = new PatternLayoutEncoder();
>>  logcatEncoder.setContext(loggerContext);
>>  logcatEncoder.setPattern("%msg%n");
>> logcatEncoder.start();
>>
>> PatternLayoutEncoder tagEncode = new PatternLayoutEncoder();
>>  tagEncode.setContext(loggerContext);
>> tagEncode.setPattern(tag);
>> tagEncode.start();
>>
>> LogcatAppender logcatAppender = new LogcatAppender();
>> logcatAppender.setContext(loggerContext);
>>  logcatAppender.setEncoder(logcatEncoder);
>> logcatAppender.setTagEncoder(tagEncode);
>> logcatAppender.start();
>>
>> // add the newly created appenders to the root logger;
>> // qualify Logger to disambiguate from org.slf4j.Logger
>>  ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger)
>> LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
>>
>> root.setLevel(level);
>>  root.addAppender(rollingFileAppender);
>> root.addAppender(logcatAppender);
>>
>> Can anyone tell me why it's not working?
>>
>> Thanks!
>> J
>>
>> _______________________________________________
>> Logback-user mailing list
>> Logback-user at qos.ch
>> http://mailman.qos.ch/mailman/listinfo/logback-user
>>
>
>
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20140708/5339590e/attachment.html>


More information about the Logback-user mailing list