use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy in project sling by apache.
the class TestLogWriter method testRotationBasedLegacyPattern.
@Test
public void testRotationBasedLegacyPattern() {
LogWriter lw = new LogWriter("foo", "target/foo", 5, "'.'yyyy-MM");
Appender<ILoggingEvent> a = createappender(lw);
assertInstanceOf(a, SlingRollingFileAppender.class);
SlingRollingFileAppender sr = (SlingRollingFileAppender) a;
assertInstanceOf(sr.getTriggeringPolicy(), TimeBasedRollingPolicy.class);
TimeBasedRollingPolicy tbrp = (TimeBasedRollingPolicy) sr.getTriggeringPolicy();
assertEquals(5, tbrp.getMaxHistory());
assertEquals("target/foo.%d{yyyy-MM}", tbrp.getFileNamePattern());
}
use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy 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.TimeBasedRollingPolicy in project qpid-broker-j by apache.
the class AppenderUtilsTest method testCreateRollingFileAppenderDailyRolling.
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();
assertTrue("Unexpected rolling policy", rollingPolicy instanceof RollingPolicyDecorator);
rollingPolicy = ((RollingPolicyDecorator) rollingPolicy).getDecorated();
assertTrue("Unexpected decorated rolling policy", rollingPolicy instanceof TimeBasedRollingPolicy);
assertEquals("Unexpected max history", MAX_HISTORY, ((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();
assertTrue("Unexpected triggering policy", triggeringPolicy instanceof AppenderUtils.DailyTriggeringPolicy);
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.TimeBasedRollingPolicy in project dubbo by alibaba.
the class LogbackContainer method doInitializer.
/**
* Initializer logback
*
* @param file
* @param level
* @param maxHistory
*/
private void doInitializer(String file, String level, int maxHistory) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.detachAndStopAllAppenders();
// appender
RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
fileAppender.setContext(loggerContext);
fileAppender.setName("application");
fileAppender.setFile(file);
fileAppender.setAppend(true);
// policy
TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
policy.setContext(loggerContext);
policy.setMaxHistory(maxHistory);
policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
policy.setParent(fileAppender);
policy.start();
fileAppender.setRollingPolicy(policy);
// encoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
encoder.start();
fileAppender.setEncoder(encoder);
fileAppender.start();
rootLogger.addAppender(fileAppender);
rootLogger.setLevel(Level.toLevel(level));
rootLogger.setAdditive(false);
}
use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy in project cdap-ingest by caskdata.
the class TailerLogUtils method getTimeLogger.
public static Logger getTimeLogger(String file) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender fileAppender = new RollingFileAppender();
fileAppender.setContext(loggerContext);
fileAppender.setFile(file);
fileAppender.setAppend(true);
TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
rollingPolicy.setContext(loggerContext);
rollingPolicy.setParent(fileAppender);
rollingPolicy.setFileNamePattern(file + "%d{yyyy-MM-dd_HH-mm}");
rollingPolicy.start();
fileAppender.setRollingPolicy(rollingPolicy);
PatternLayoutEncoder layoutEncoder = new PatternLayoutEncoder();
layoutEncoder.setContext(loggerContext);
layoutEncoder.setPattern("[%d %-5p %c{1}] %msg%n");
layoutEncoder.start();
fileAppender.setEncoder(layoutEncoder);
fileAppender.start();
// configures the root logger
Logger rootLogger = loggerContext.getLogger(BaseTailerTest.class.getName() + "time");
rootLogger.setLevel(Level.DEBUG);
rootLogger.addAppender(fileAppender);
return rootLogger;
}
Aggregations