Search in sources :

Example 11 with TimeBasedRollingPolicy

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());
}
Also used : SlingRollingFileAppender(org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Test(org.junit.Test)

Example 12 with TimeBasedRollingPolicy

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);
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) File(java.io.File) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) TriggeringPolicy(ch.qos.logback.core.rolling.TriggeringPolicy) RollingPolicyBase(ch.qos.logback.core.rolling.RollingPolicyBase) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 13 with TimeBasedRollingPolicy

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());
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) RollingPolicy(ch.qos.logback.core.rolling.RollingPolicy) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) TriggeringPolicy(ch.qos.logback.core.rolling.TriggeringPolicy) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 14 with TimeBasedRollingPolicy

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);
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 15 with TimeBasedRollingPolicy

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;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Aggregations

TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)19 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)15 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)13 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)11 LoggerContext (ch.qos.logback.classic.LoggerContext)10 Logger (ch.qos.logback.classic.Logger)7 Test (org.junit.Test)5 RollingPolicy (ch.qos.logback.core.rolling.RollingPolicy)4 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)2 TriggeringPolicy (ch.qos.logback.core.rolling.TriggeringPolicy)2 GelfAppender (de.appelgriepsch.logback.GelfAppender)2 File (java.io.File)2 SlingRollingFileAppender (org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 LogcatAppender (ch.qos.logback.classic.android.LogcatAppender)1 Appender (ch.qos.logback.core.Appender)1 FileAppender (ch.qos.logback.core.FileAppender)1 LayoutWrappingEncoder (ch.qos.logback.core.encoder.LayoutWrappingEncoder)1 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)1 RollingPolicyBase (ch.qos.logback.core.rolling.RollingPolicyBase)1