Search in sources :

Example 6 with SizeBasedTriggeringPolicy

use of ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy in project accent4j by cinchapi.

the class Logger method setup.

/**
 * Programmatically configure an internal logger.
 *
 * @param name the name of the internal logger
 * @param file the the internal logger uses
 * @return the internal logger
 */
private ch.qos.logback.classic.Logger setup(String name, String file) {
    if (!enableConsoleLogging) {
        ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
        root.detachAndStopAllAppenders();
    }
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    // Configure Pattern
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setPattern("%date [%thread] %level - %msg%n");
    encoder.setContext(context);
    encoder.start();
    // Create File Appender
    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<ILoggingEvent>();
    appender.setFile(directory + File.separator + file);
    appender.setContext(context);
    // Configure Rolling Policy
    FixedWindowRollingPolicy rolling = new FixedWindowRollingPolicy();
    rolling.setMaxIndex(1);
    rolling.setMaxIndex(5);
    rolling.setContext(context);
    rolling.setFileNamePattern(directory + File.separator + file + ".%i.zip");
    rolling.setParent(appender);
    rolling.start();
    // Configure Triggering Policy
    SizeBasedTriggeringPolicy<ILoggingEvent> triggering = new SizeBasedTriggeringPolicy<ILoggingEvent>();
    triggering.setMaxFileSize(maxFileSize);
    triggering.start();
    // Configure File Appender
    appender.setEncoder(encoder);
    appender.setRollingPolicy(rolling);
    appender.setTriggeringPolicy(triggering);
    appender.start();
    // Get Logger
    ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(name);
    logger.addAppender(appender);
    logger.setLevel(level);
    logger.setAdditive(true);
    return logger;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)

Example 7 with SizeBasedTriggeringPolicy

use of ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy in project jmxtrans by jmxtrans.

the class KeyOutWriter method buildTriggeringPolicy.

protected TriggeringPolicy buildTriggeringPolicy() {
    SizeBasedTriggeringPolicy triggeringPolicy = new SizeBasedTriggeringPolicy();
    triggeringPolicy.setContext(loggerContext);
    triggeringPolicy.setMaxFileSize(FileSize.valueOf(maxLogFileSize));
    triggeringPolicy.start();
    return triggeringPolicy;
}
Also used : SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)

Example 8 with SizeBasedTriggeringPolicy

use of ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy 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

SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)8 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)7 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)5 LoggerContext (ch.qos.logback.classic.LoggerContext)4 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)3 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)3 FileAppender (ch.qos.logback.core.FileAppender)2 SlingRollingFileAppender (org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender)2 Test (org.junit.Test)2 Logger (ch.qos.logback.classic.Logger)1 Appender (ch.qos.logback.core.Appender)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 LayoutWrappingEncoder (ch.qos.logback.core.encoder.LayoutWrappingEncoder)1 DefaultTimeBasedFileNamingAndTriggeringPolicy (ch.qos.logback.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy)1 SizeAndTimeBasedRollingPolicy (ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy)1 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)1 FileSize (ch.qos.logback.core.util.FileSize)1 Matcher (java.util.regex.Matcher)1 SlingContextUtil (org.apache.sling.commons.log.logback.internal.util.SlingContextUtil)1