Search in sources :

Example 6 with SizeAndTimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy in project gocd by gocd.

the class LogHelper method rollingPolicyForAppender.

public static void rollingPolicyForAppender(RollingFileAppender rollingFileAppender, String maxFileSize, String totalSizeCap, int maxHistory) {
    SizeAndTimeBasedRollingPolicy rollingPolicy = new SizeAndTimeBasedRollingPolicy();
    rollingPolicy.setContext(LOGGER_CONTEXT);
    rollingPolicy.setMaxHistory(maxHistory);
    rollingPolicy.setMaxFileSize(FileSize.valueOf(maxFileSize));
    rollingPolicy.setTotalSizeCap(FileSize.valueOf(totalSizeCap));
    rollingPolicy.setFileNamePattern(rollingFileAppender.rawFileProperty() + ".%d{yyyy-MM-dd}.%i.gz");
    rollingPolicy.setParent(rollingFileAppender);
    rollingFileAppender.setRollingPolicy(rollingPolicy);
    rollingPolicy.start();
}
Also used : SizeAndTimeBasedRollingPolicy(ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy)

Example 7 with SizeAndTimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy in project dropwizard by dropwizard.

the class FileAppenderFactory method buildAppender.

protected FileAppender<E> buildAppender(LoggerContext context) {
    if (archive) {
        final RollingFileAppender<E> appender = new RollingFileAppender<>();
        appender.setContext(context);
        appender.setFile(currentLogFilename);
        appender.setBufferSize(new FileSize(bufferSize.toBytes()));
        if (maxFileSize != null && !requireNonNull(archivedLogFilenamePattern).contains("%d")) {
            final FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
            rollingPolicy.setContext(context);
            rollingPolicy.setMaxIndex(getArchivedFileCount());
            rollingPolicy.setFileNamePattern(getArchivedLogFilenamePattern());
            rollingPolicy.setParent(appender);
            rollingPolicy.start();
            appender.setRollingPolicy(rollingPolicy);
            final SizeBasedTriggeringPolicy<E> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
            triggeringPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
            triggeringPolicy.setContext(context);
            triggeringPolicy.start();
            appender.setTriggeringPolicy(triggeringPolicy);
            return appender;
        } else {
            final TimeBasedRollingPolicy<E> rollingPolicy;
            if (maxFileSize == null) {
                rollingPolicy = new TimeBasedRollingPolicy<>();
                final TimeBasedFileNamingAndTriggeringPolicy<E> triggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<>();
                triggeringPolicy.setContext(context);
                triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy);
                appender.setTriggeringPolicy(triggeringPolicy);
            } else {
                // Creating a size and time policy does not need a separate triggering policy set
                // on the appender because this policy registers the trigger policy
                final SizeAndTimeBasedRollingPolicy<E> sizeAndTimeBasedRollingPolicy = new SizeAndTimeBasedRollingPolicy<>();
                sizeAndTimeBasedRollingPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
                rollingPolicy = sizeAndTimeBasedRollingPolicy;
            }
            if (totalSizeCap != null) {
                rollingPolicy.setTotalSizeCap(new FileSize(totalSizeCap.toBytes()));
            }
            rollingPolicy.setContext(context);
            rollingPolicy.setFileNamePattern(archivedLogFilenamePattern);
            rollingPolicy.setMaxHistory(archivedFileCount);
            appender.setRollingPolicy(rollingPolicy);
            rollingPolicy.setParent(appender);
            rollingPolicy.start();
            return appender;
        }
    }
    final FileAppender<E> appender = new FileAppender<>();
    appender.setContext(context);
    appender.setFile(currentLogFilename);
    appender.setBufferSize(new FileSize(bufferSize.toBytes()));
    return appender;
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) DefaultTimeBasedFileNamingAndTriggeringPolicy(ch.qos.logback.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeAndTimeBasedRollingPolicy(ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) FileSize(ch.qos.logback.core.util.FileSize)

Aggregations

SizeAndTimeBasedRollingPolicy (ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy)7 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)5 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)4 LoggerContext (ch.qos.logback.classic.LoggerContext)2 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)2 RollingPolicy (ch.qos.logback.core.rolling.RollingPolicy)2 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)2 FileSize (ch.qos.logback.core.util.FileSize)2 File (java.io.File)2 Test (org.junit.jupiter.api.Test)2 AsyncAppender (ch.qos.logback.classic.AsyncAppender)1 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)1 Appender (ch.qos.logback.core.Appender)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 FileAppender (ch.qos.logback.core.FileAppender)1 DefaultTimeBasedFileNamingAndTriggeringPolicy (ch.qos.logback.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy)1 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)1 FileNamePattern (ch.qos.logback.core.rolling.helper.FileNamePattern)1 ArrayList (java.util.ArrayList)1