Search in sources :

Example 1 with SizeAndTimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy in project spring-boot by spring-projects.

the class LogbackConfigurationTests method defaultRollingFileNamePattern.

@Test
void defaultRollingFileNamePattern() throws JoranException {
    JoranConfigurator configurator = new JoranConfigurator();
    LoggerContext context = new LoggerContext();
    configurator.setContext(context);
    configurator.doConfigure(new File("src/test/resources/custom-file-log-pattern.xml"));
    Appender<ILoggingEvent> appender = context.getLogger("ROOT").getAppender("FILE");
    assertThat(appender).isInstanceOf(RollingFileAppender.class);
    RollingPolicy rollingPolicy = ((RollingFileAppender<?>) appender).getRollingPolicy();
    String fileNamePattern = ((SizeAndTimeBasedRollingPolicy<?>) rollingPolicy).getFileNamePattern();
    assertThat(fileNamePattern).endsWith("spring.log.%d{yyyy-MM-dd}.%i.gz");
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeAndTimeBasedRollingPolicy(ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) SizeAndTimeBasedRollingPolicy(ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy) RollingPolicy(ch.qos.logback.core.rolling.RollingPolicy) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with SizeAndTimeBasedRollingPolicy

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

the class DailyKeyOutWriter method buildRollingPolicy.

@Override
protected RollingPolicy buildRollingPolicy(FileAppender<?> appender, String fileStr) {
    SizeAndTimeBasedRollingPolicy rollingPolicy = new SizeAndTimeBasedRollingPolicy();
    rollingPolicy.setParent(appender);
    rollingPolicy.setContext(loggerContext);
    if (getMaxLogFileSize() == null) {
        rollingPolicy.setFileNamePattern(fileStr + "%d{" + datePattern + "}");
    } else {
        rollingPolicy.setMaxFileSize(FileSize.valueOf(getMaxLogFileSize()));
        rollingPolicy.setFileNamePattern(fileStr + "%d{" + datePattern + "}.%i");
    }
    rollingPolicy.setMaxHistory(getMaxLogBackupFiles());
    rollingPolicy.start();
    return rollingPolicy;
}
Also used : SizeAndTimeBasedRollingPolicy(ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy)

Example 3 with SizeAndTimeBasedRollingPolicy

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

the class CoreLogback method fileAppender.

protected static RollingFileAppender<ILoggingEvent> fileAppender(String name, LoggerContext context, LayoutWrappingEncoder<ILoggingEvent> encoder, TTLLLayout layout, CoreConfig.Log.Output.Type.File outputType) {
    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    appender.setContext(context);
    appender.setName(name);
    appender.setAppend(true);
    String logPath = outputType.asFile().path().resolve(TYPEDB_LOG_FILE).toAbsolutePath().toString();
    appender.setFile(logPath);
    appender.setLayout(layout);
    appender.setEncoder(encoder);
    SizeAndTimeBasedRollingPolicy<?> policy = new SizeAndTimeBasedRollingPolicy<>();
    policy.setContext(context);
    policy.setFileNamePattern(logPath + TYPEDB_LOG_FILE_ARCHIVE_SUFFIX);
    long directorySize = outputType.fileSizeCap() + outputType.archivesSizeCap();
    policy.setMaxFileSize(new FileSize(outputType.fileSizeCap()));
    policy.setTotalSizeCap(new FileSize(directorySize));
    policy.setParent(appender);
    policy.start();
    appender.setRollingPolicy(policy);
    appender.start();
    return appender;
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeAndTimeBasedRollingPolicy(ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) FileSize(ch.qos.logback.core.util.FileSize)

Example 4 with SizeAndTimeBasedRollingPolicy

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

the class AionLoggerFactory method constructAppenders.

private static List<Appender<ILoggingEvent>> constructAppenders(boolean shouldLogToFile, String _logDirectory) {
    List<Appender<ILoggingEvent>> appenders = new ArrayList<>();
    String logDirectory = DEFAULT_LOG_DIR;
    if (_logDirectory != null && !_logDirectory.trim().isEmpty())
        logDirectory = _logDirectory;
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(context);
    encoder.setPattern(DEFAULT_LOG_PATTERN);
    encoder.start();
    ConsoleAppender<ILoggingEvent> consoleSync = new ConsoleAppender<>();
    consoleSync.setContext(context);
    // for logger debugging
    consoleSync.setName("consoleSyncAppender");
    consoleSync.setEncoder(encoder);
    consoleSync.start();
    AsyncAppender consoleAsync = new AsyncAppender();
    consoleAsync.setContext(context);
    // for logger debugging
    consoleAsync.setName("consoleAsyncAppender");
    consoleAsync.addAppender(consoleSync);
    consoleAsync.setIncludeCallerData(ASYNC_LOGGER_INCLUDE_CALLER_DATA);
    consoleAsync.setNeverBlock(ASYNC_LOGGER_NEVER_BLOCK);
    consoleAsync.setMaxFlushTime(ASYNC_LOGGER_MAX_FLUSH_TIME_MS);
    consoleAsync.setDiscardingThreshold(ASYNC_LOGGER_DISCARDING_THRESHOLD);
    consoleAsync.setQueueSize(ASYNC_LOGGER_QUEUE_SIZE);
    consoleAsync.start();
    appenders.add(consoleAsync);
    if (!shouldLogToFile)
        return appenders;
    RollingFileAppender<ILoggingEvent> fileSync = new RollingFileAppender<>();
    SizeBasedTriggeringPolicy<ILoggingEvent> tp = new SizeBasedTriggeringPolicy<>();
    tp.setContext(context);
    tp.start();
    SizeAndTimeBasedRollingPolicy rp = new SizeAndTimeBasedRollingPolicy();
    rp.setContext(context);
    // roll-over each day
    // notice that we don't use the OS-agnostic File.separator here since logback is converts
    // the FileNamePattern
    // to a unix-style path using ch.qos.logback.core.rolling.helper.FileFilterUtil.slashify
    FileNamePattern fnp = new FileNamePattern(logDirectory + "/%d{yyyy/MM, aux}/aion.%d{yyyy-MM-dd}.%i.log", context);
    rp.setFileNamePattern(fnp.getPattern());
    // max rollover file size = 100MB
    rp.setMaxFileSize(FileSize.valueOf("100mb"));
    rp.setParent(fileSync);
    rp.start();
    // for logger debugging
    fileSync.setName("fileSyncAppender");
    fileSync.setContext(context);
    fileSync.setTriggeringPolicy(tp);
    fileSync.setRollingPolicy(rp);
    fileSync.setFile(logDirectory + File.separator + DEFAULT_LOG_FILE_CURRENT);
    fileSync.setEncoder(encoder);
    fileSync.setAppend(true);
    fileSync.start();
    AsyncAppender fileAsync = new AsyncAppender();
    fileAsync.setContext(context);
    // for logger debugging
    fileAsync.setName("fileAsyncAppender");
    fileAsync.addAppender(fileSync);
    fileAsync.setIncludeCallerData(ASYNC_LOGGER_INCLUDE_CALLER_DATA);
    fileAsync.setNeverBlock(ASYNC_LOGGER_NEVER_BLOCK);
    fileAsync.setMaxFlushTime(ASYNC_LOGGER_MAX_FLUSH_TIME_MS);
    fileAsync.setDiscardingThreshold(ASYNC_LOGGER_DISCARDING_THRESHOLD);
    fileAsync.setQueueSize(ASYNC_LOGGER_QUEUE_SIZE);
    fileAsync.start();
    appenders.add(fileAsync);
    return appenders;
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Appender(ch.qos.logback.core.Appender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) AsyncAppender(ch.qos.logback.classic.AsyncAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeAndTimeBasedRollingPolicy(ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy) ArrayList(java.util.ArrayList) FileNamePattern(ch.qos.logback.core.rolling.helper.FileNamePattern) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) AsyncAppender(ch.qos.logback.classic.AsyncAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)

Example 5 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