use of ch.qos.logback.core.rolling.TriggeringPolicy in project qpid-broker-j by apache.
the class AppenderUtilsTest method testCreateRollingFileAppenderNonDailyRolling.
@Test
public void testCreateRollingFileAppenderNonDailyRolling() {
when(_settings.isRollDaily()).thenReturn(Boolean.FALSE);
when(_settings.isCompressOldFiles()).thenReturn(Boolean.FALSE);
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
AppenderUtils.configureRollingFileAppender(_settings, new LoggerContext(), appender);
assertEquals("Unexpected appender file name", _testLogFileName, appender.getFile());
RollingPolicy rollingPolicy = appender.getRollingPolicy();
final boolean condition2 = rollingPolicy instanceof RollingPolicyDecorator;
assertTrue("Unexpected rolling policy", condition2);
rollingPolicy = ((RollingPolicyDecorator) rollingPolicy).getDecorated();
final boolean condition1 = rollingPolicy instanceof AppenderUtils.SimpleRollingPolicy;
assertTrue("Unexpected decorated rolling policy", condition1);
assertEquals("Unexpected max history", (long) MAX_HISTORY, (long) ((AppenderUtils.SimpleRollingPolicy) rollingPolicy).getMaxIndex());
assertEquals("Unexpected file name pattern", _testLogFileName + ".%i", ((AppenderUtils.SimpleRollingPolicy) rollingPolicy).getFileNamePattern());
assertEquals("Unexpected compression mode", CompressionMode.NONE, rollingPolicy.getCompressionMode());
TriggeringPolicy triggeringPolicy = appender.getTriggeringPolicy();
assertEquals("Unexpected triggering policy", String.valueOf(MAX_FILE_SIZE) + " MB", ((AppenderUtils.SizeTriggeringPolicy) triggeringPolicy).getMaxFileSize().toString());
Encoder encoder = appender.getEncoder();
final boolean condition = encoder instanceof PatternLayoutEncoder;
assertTrue("Unexpected encoder", condition);
assertEquals("Unexpected layout pattern", LAYOUT, ((PatternLayoutEncoder) encoder).getPattern());
}
use of ch.qos.logback.core.rolling.TriggeringPolicy in project qpid-broker-j by apache.
the class AppenderUtilsTest method testCreateRollingFileAppenderDailyRolling.
@Test
public void testCreateRollingFileAppenderDailyRolling() {
final RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
AppenderUtils.configureRollingFileAppender(_settings, new LoggerContext(), appender);
assertEquals("Unexpected appender file name", _testLogFileName, appender.getFile());
RollingPolicy rollingPolicy = appender.getRollingPolicy();
final boolean condition2 = rollingPolicy instanceof RollingPolicyDecorator;
assertTrue("Unexpected rolling policy", condition2);
rollingPolicy = ((RollingPolicyDecorator) rollingPolicy).getDecorated();
final boolean condition1 = rollingPolicy instanceof TimeBasedRollingPolicy;
assertTrue("Unexpected decorated rolling policy", condition1);
assertEquals("Unexpected max history", (long) MAX_HISTORY, (long) ((TimeBasedRollingPolicy) rollingPolicy).getMaxHistory());
assertEquals("Unexpected file name pattern", _testLogFileName + ".%d{yyyy-MM-dd}.%i.gz", ((TimeBasedRollingPolicy) rollingPolicy).getFileNamePattern());
assertEquals("Unexpected compression mode", CompressionMode.GZ, rollingPolicy.getCompressionMode());
TriggeringPolicy triggeringPolicy = ((TimeBasedRollingPolicy) rollingPolicy).getTimeBasedFileNamingAndTriggeringPolicy();
final boolean condition = triggeringPolicy instanceof AppenderUtils.DailyTriggeringPolicy;
assertTrue("Unexpected triggering policy", condition);
assertEquals("Unexpected triggering policy", String.valueOf(MAX_FILE_SIZE) + " MB", ((AppenderUtils.DailyTriggeringPolicy) triggeringPolicy).getMaxFileSize().toString());
assertEquals("Unexpected layout", LAYOUT, ((PatternLayoutEncoder) appender.getEncoder()).getPattern());
}
use of ch.qos.logback.core.rolling.TriggeringPolicy in project qpid-broker-j by apache.
the class AppenderUtils method configureRollingFileAppender.
public static void configureRollingFileAppender(FileLoggerSettings fileLoggerSettings, Context loggerContext, RollingFileAppender<ILoggingEvent> appender) {
String fileName = fileLoggerSettings.getFileName();
File file = new File(fileName);
if (file.getParentFile() != null) {
file.getParentFile().mkdirs();
}
validateLogFilePermissions(file);
validateMaxFileSize(fileLoggerSettings.getMaxFileSize());
appender.setFile(fileName);
appender.setAppend(true);
appender.setContext(loggerContext);
TriggeringPolicy triggeringPolicy;
RollingPolicyBase rollingPolicy;
final String maxFileSizeAsString = String.valueOf(fileLoggerSettings.getMaxFileSize()) + "MB";
if (fileLoggerSettings.isRollDaily()) {
DailyTriggeringPolicy dailyTriggeringPolicy = new DailyTriggeringPolicy(fileLoggerSettings.isRollOnRestart(), maxFileSizeAsString);
dailyTriggeringPolicy.setContext(loggerContext);
TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<>();
timeBasedRollingPolicy.setMaxHistory(fileLoggerSettings.getMaxHistory());
timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(dailyTriggeringPolicy);
timeBasedRollingPolicy.setFileNamePattern(fileName + ".%d{yyyy-MM-dd}.%i" + (fileLoggerSettings.isCompressOldFiles() ? ".gz" : ""));
rollingPolicy = timeBasedRollingPolicy;
triggeringPolicy = dailyTriggeringPolicy;
} else {
SizeTriggeringPolicy sizeTriggeringPolicy = new SizeTriggeringPolicy(fileLoggerSettings.isRollOnRestart(), maxFileSizeAsString);
sizeTriggeringPolicy.setContext(loggerContext);
SimpleRollingPolicy simpleRollingPolicy = new SimpleRollingPolicy(fileLoggerSettings.getMaxHistory());
simpleRollingPolicy.setFileNamePattern(fileName + ".%i" + (fileLoggerSettings.isCompressOldFiles() ? ".gz" : ""));
rollingPolicy = simpleRollingPolicy;
triggeringPolicy = sizeTriggeringPolicy;
}
rollingPolicy.setContext(loggerContext);
RollingPolicyDecorator decorator = new RollingPolicyDecorator(rollingPolicy, fileLoggerSettings.getRolloverListener(), fileLoggerSettings.getExecutorService());
decorator.setParent(appender);
appender.setRollingPolicy(decorator);
appender.setTriggeringPolicy(triggeringPolicy);
decorator.start();
triggeringPolicy.start();
final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setPattern(fileLoggerSettings.getLayout());
encoder.setContext(loggerContext);
encoder.start();
appender.setEncoder(encoder);
}
use of ch.qos.logback.core.rolling.TriggeringPolicy in project jmxtrans by jmxtrans.
the class KeyOutWriter method buildAppender.
protected Appender buildAppender(String loggerName, String fileStr) {
final RollingFileAppender appender = new RollingFileAppender();
appender.setName(loggerName + "RollingFile");
appender.setContext(loggerContext);
appender.setImmediateFlush(true);
appender.setBufferSize(new FileSize(LOG_IO_BUFFER_SIZE_BYTES));
appender.setFile(fileStr);
appender.setEncoder(buildEncoder());
TriggeringPolicy triggeringPolicy = buildTriggeringPolicy();
if (triggeringPolicy != null) {
appender.setTriggeringPolicy(triggeringPolicy);
}
appender.setRollingPolicy(buildRollingPolicy(appender, fileStr));
appender.start();
return appender;
}
Aggregations