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;
}
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);
}
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());
}
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);
}
Aggregations