Search in sources :

Example 1 with FileSize

use of ch.qos.logback.core.util.FileSize 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.setFile(currentLogFilename);
        appender.setBufferSize(new FileSize(bufferSize.toBytes()));
        if (maxFileSize != null && !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 TimeBasedFileNamingAndTriggeringPolicy<E> triggeringPolicy;
            if (maxFileSize == null) {
                triggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<>();
            } else {
                final SizeAndTimeBasedFNATP<E> maxFileSizeTriggeringPolicy = new SizeAndTimeBasedFNATP<>();
                maxFileSizeTriggeringPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
                triggeringPolicy = maxFileSizeTriggeringPolicy;
            }
            triggeringPolicy.setContext(context);
            final TimeBasedRollingPolicy<E> rollingPolicy = new TimeBasedRollingPolicy<>();
            rollingPolicy.setContext(context);
            rollingPolicy.setFileNamePattern(archivedLogFilenamePattern);
            rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
            triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy);
            rollingPolicy.setMaxHistory(archivedFileCount);
            appender.setRollingPolicy(rollingPolicy);
            appender.setTriggeringPolicy(triggeringPolicy);
            rollingPolicy.setParent(appender);
            rollingPolicy.start();
            return appender;
        }
    }
    final FileAppender<E> appender = new FileAppender<>();
    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) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) SizeAndTimeBasedFNATP(ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) FileSize(ch.qos.logback.core.util.FileSize)

Example 2 with FileSize

use of ch.qos.logback.core.util.FileSize in project dropwizard by dropwizard.

the class FileAppenderFactoryTest method hasMaxFileSize.

@Test
public void hasMaxFileSize() throws Exception {
    FileAppenderFactory fileAppenderFactory = new FileAppenderFactory();
    fileAppenderFactory.setCurrentLogFilename(folder.newFile("logfile.log").toString());
    fileAppenderFactory.setArchive(true);
    fileAppenderFactory.setMaxFileSize(Size.kilobytes(1));
    fileAppenderFactory.setArchivedLogFilenamePattern(folder.newFile("example-%d-%i.log.gz").toString());
    RollingFileAppender<ILoggingEvent> appender = (RollingFileAppender<ILoggingEvent>) fileAppenderFactory.buildAppender(new LoggerContext());
    assertThat(appender.getTriggeringPolicy()).isInstanceOf(SizeAndTimeBasedFNATP.class);
    final Field maxFileSizeField = SizeAndTimeBasedFNATP.class.getDeclaredField("maxFileSize");
    maxFileSizeField.setAccessible(true);
    final FileSize maxFileSize = (FileSize) maxFileSizeField.get(appender.getTriggeringPolicy());
    assertThat(maxFileSize.getSize()).isEqualTo(1024L);
}
Also used : Field(java.lang.reflect.Field) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) FileSize(ch.qos.logback.core.util.FileSize) Test(org.junit.Test)

Example 3 with FileSize

use of ch.qos.logback.core.util.FileSize in project dropwizard by dropwizard.

the class FileAppenderFactoryTest method overrideBufferSize.

@Test
public void overrideBufferSize() throws NoSuchFieldException, IllegalAccessException {
    FileAppenderFactory<ILoggingEvent> fileAppenderFactory = new FileAppenderFactory<>();
    fileAppenderFactory.setArchive(false);
    fileAppenderFactory.setBufferSize(Size.kilobytes(256));
    AsyncAppender asyncAppender = (AsyncAppender) fileAppenderFactory.build(new LoggerContext(), "test", new DropwizardLayoutFactory(), new NullLevelFilterFactory<>(), new AsyncLoggingEventAppenderFactory());
    final Appender<ILoggingEvent> fileAppender = asyncAppender.getAppender("file-appender");
    assertThat(fileAppender).isInstanceOf(FileAppender.class);
    final Field bufferSizeField = FileAppender.class.getDeclaredField("bufferSize");
    bufferSizeField.setAccessible(true);
    FileSize bufferSizeFromAppender = (FileSize) bufferSizeField.get(fileAppender);
    assertThat(bufferSizeFromAppender.getSize()).isEqualTo(fileAppenderFactory.getBufferSize().toBytes());
}
Also used : Field(java.lang.reflect.Field) AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) AsyncAppender(ch.qos.logback.classic.AsyncAppender) NullLevelFilterFactory(io.dropwizard.logging.filter.NullLevelFilterFactory) DropwizardLayoutFactory(io.dropwizard.logging.layout.DropwizardLayoutFactory) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) FileSize(ch.qos.logback.core.util.FileSize) Test(org.junit.Test)

Example 4 with FileSize

use of ch.qos.logback.core.util.FileSize in project dropwizard by dropwizard.

the class FileAppenderFactoryTest method hasMaxFileSizeFixedWindow.

@Test
public void hasMaxFileSizeFixedWindow() throws Exception {
    FileAppenderFactory fileAppenderFactory = new FileAppenderFactory();
    fileAppenderFactory.setCurrentLogFilename(folder.newFile("logfile.log").toString());
    fileAppenderFactory.setArchive(true);
    fileAppenderFactory.setMaxFileSize(Size.kilobytes(1));
    fileAppenderFactory.setArchivedLogFilenamePattern(folder.newFile("example-%i.log.gz").toString());
    RollingFileAppender<ILoggingEvent> appender = (RollingFileAppender<ILoggingEvent>) fileAppenderFactory.buildAppender(new LoggerContext());
    assertThat(appender.getRollingPolicy()).isInstanceOf(FixedWindowRollingPolicy.class);
    assertThat(appender.getRollingPolicy().isStarted()).isTrue();
    assertThat(appender.getTriggeringPolicy()).isInstanceOf(SizeBasedTriggeringPolicy.class);
    assertThat(appender.getTriggeringPolicy().isStarted()).isTrue();
    final Field maxFileSizeField = SizeBasedTriggeringPolicy.class.getDeclaredField("maxFileSize");
    maxFileSizeField.setAccessible(true);
    final FileSize maxFileSize = (FileSize) maxFileSizeField.get(appender.getTriggeringPolicy());
    assertThat(maxFileSize.getSize()).isEqualTo(1024L);
}
Also used : Field(java.lang.reflect.Field) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) FileSize(ch.qos.logback.core.util.FileSize) Test(org.junit.Test)

Aggregations

FileSize (ch.qos.logback.core.util.FileSize)4 LoggerContext (ch.qos.logback.classic.LoggerContext)3 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)3 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)3 Field (java.lang.reflect.Field)3 Test (org.junit.Test)3 AsyncAppender (ch.qos.logback.classic.AsyncAppender)1 FileAppender (ch.qos.logback.core.FileAppender)1 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)1 SizeAndTimeBasedFNATP (ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP)1 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)1 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)1 AsyncLoggingEventAppenderFactory (io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory)1 NullLevelFilterFactory (io.dropwizard.logging.filter.NullLevelFilterFactory)1 DropwizardLayoutFactory (io.dropwizard.logging.layout.DropwizardLayoutFactory)1