Search in sources :

Example 6 with FixedWindowRollingPolicy

use of ch.qos.logback.core.rolling.FixedWindowRollingPolicy in project sling by apache.

the class TestLogWriter method testSizeBasedLegacyPattern.

@Test
public void testSizeBasedLegacyPattern() {
    LogWriter lw = new LogWriter("foo", "target/foo", 5, "4k");
    Appender<ILoggingEvent> a = createappender(lw);
    assertInstanceOf(a, SlingRollingFileAppender.class);
    SlingRollingFileAppender sr = (SlingRollingFileAppender) a;
    assertInstanceOf(sr.getTriggeringPolicy(), SizeBasedTriggeringPolicy.class);
    assertInstanceOf(sr.getRollingPolicy(), FixedWindowRollingPolicy.class);
    SizeBasedTriggeringPolicy sbtp = (SizeBasedTriggeringPolicy) sr.getTriggeringPolicy();
    FixedWindowRollingPolicy fwRp = (FixedWindowRollingPolicy) sr.getRollingPolicy();
    assertEquals(5, fwRp.getMaxIndex());
    assertEquals(String.valueOf(4 * FileUtils.ONE_KB), sbtp.getMaxFileSize());
}
Also used : FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) SlingRollingFileAppender(org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Test(org.junit.Test)

Example 7 with FixedWindowRollingPolicy

use of ch.qos.logback.core.rolling.FixedWindowRollingPolicy in project bitsquare by bitsquare.

the class Log method setup.

public static void setup(String fileName) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender appender = new RollingFileAppender();
    appender.setContext(loggerContext);
    appender.setFile(fileName + ".log");
    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(appender);
    rollingPolicy.setFileNamePattern(fileName + "_%i.log");
    rollingPolicy.setMinIndex(1);
    rollingPolicy.setMaxIndex(10);
    rollingPolicy.start();
    triggeringPolicy = new SizeBasedTriggeringPolicy();
    triggeringPolicy.setMaxFileSize(FileSize.valueOf("10MB"));
    triggeringPolicy.start();
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern("%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15}: %msg %xEx%n");
    encoder.start();
    appender.setEncoder(encoder);
    appender.setRollingPolicy(rollingPolicy);
    appender.setTriggeringPolicy(triggeringPolicy);
    appender.start();
    logbackLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    logbackLogger.addAppender(appender);
    logbackLogger.setLevel(Level.INFO);
// log errors in separate file
// not working as expected still.... damn logback...
/* FileAppender errorAppender = new FileAppender();
        errorAppender.setEncoder(encoder);
        errorAppender.setName("Error");
        errorAppender.setContext(loggerContext);
        errorAppender.setFile(fileName + "_error.log");
        LevelFilter levelFilter = new LevelFilter();
        levelFilter.setLevel(Level.ERROR);
        levelFilter.setOnMatch(FilterReply.ACCEPT);
        levelFilter.setOnMismatch(FilterReply.DENY);
        levelFilter.start();
        errorAppender.addFilter(levelFilter);
        errorAppender.start();
        logbackLogger.addAppender(errorAppender);*/
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) LoggerContext(ch.qos.logback.classic.LoggerContext)

Aggregations

FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)7 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)6 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)4 LoggerContext (ch.qos.logback.classic.LoggerContext)3 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)2 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)2 FileAppender (ch.qos.logback.core.FileAppender)2 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)2 SlingRollingFileAppender (org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender)2 Test (org.junit.Test)2 Logger (ch.qos.logback.classic.Logger)1 Appender (ch.qos.logback.core.Appender)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 LayoutWrappingEncoder (ch.qos.logback.core.encoder.LayoutWrappingEncoder)1 SizeAndTimeBasedFNATP (ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP)1 FileSize (ch.qos.logback.core.util.FileSize)1 Matcher (java.util.regex.Matcher)1 SlingContextUtil (org.apache.sling.commons.log.logback.internal.util.SlingContextUtil)1