use of ch.qos.logback.core.rolling.RollingFileAppender in project coffeenet-starter by coffeenet.
the class CoffeeNetLoggingAutoConfigurationTest method integration.
@Test
public void integration() {
final Logger logger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME);
assertThat(logger.getAppender("CONSOLE"), is(instanceOf(ConsoleAppender.class)));
Appender<ILoggingEvent> fileAppender = logger.getAppender("COFFEENET-FILE");
assertThat(fileAppender, is(instanceOf(RollingFileAppender.class)));
// Check File Appender
RollingFileAppender rollingFileAppender = (RollingFileAppender) fileAppender;
assertThat(rollingFileAppender.getFile(), is("logs/app.log"));
assertThat(rollingFileAppender.getName(), is("COFFEENET-FILE"));
RollingPolicy rollingPolicy = rollingFileAppender.getRollingPolicy();
assertThat(rollingPolicy, is(instanceOf(TimeBasedRollingPolicy.class)));
TimeBasedRollingPolicy timeBasedRollingPolicy = (TimeBasedRollingPolicy) rollingFileAppender.getRollingPolicy();
assertThat(timeBasedRollingPolicy.getFileNamePattern(), is("logs/app-%d{yyyy-MM-dd}.log"));
PatternLayoutEncoder encoder = (PatternLayoutEncoder) rollingFileAppender.getEncoder();
assertThat(encoder.getPattern(), is("%d{yyyy-MM-dd HH:mm:ss.SSS} %5p --- [%t] %-40.40logger{39} : %m%n%wEx"));
// Check Gelf Appender
Appender<ILoggingEvent> gelfAppender = logger.getAppender("COFFEENET-GELF");
assertThat(gelfAppender, is(instanceOf(GelfAppender.class)));
GelfAppender gelfAppender1 = (GelfAppender) gelfAppender;
assertThat(gelfAppender1.getName(), is("COFFEENET-GELF"));
assertThat(gelfAppender1.getServer(), is("localServer"));
assertThat(gelfAppender1.getProtocol(), is("TCP"));
assertThat(gelfAppender1.getPort(), is(1337));
assertThat(((PatternLayout) gelfAppender1.getLayout()).getPattern(), is("%m"));
assertThat(gelfAppender1.getAdditionalFields(), hasEntry("environment", "test"));
assertThat(gelfAppender1.getAdditionalFields(), hasEntry("application", "CoffeeNetApplication"));
}
use of ch.qos.logback.core.rolling.RollingFileAppender 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.rolling.RollingFileAppender 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.rolling.RollingFileAppender 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);
}
use of ch.qos.logback.core.rolling.RollingFileAppender in project jmxtrans by jmxtrans.
the class TimeBasedRollingKeyOutWriter method initLogger.
@Override
protected Logger initLogger(String fileStr) throws IOException {
RollingPolicy rollingPolicy = initRollingPolicy(fileStr, getMaxLogBackupFiles(), getMaxLogFileSize());
RollingFileAppender appender = buildAppender(buildEncoder(), rollingPolicy);
rollingPolicy.start();
appender.start();
// configure the logger for info and add the appender
return getAndConfigureLogger(appender);
}
Aggregations