Search in sources :

Example 1 with SizeBasedTriggeringPolicy

use of ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy in project cdap-ingest by caskdata.

the class TailerLogUtils method getSizeLogger.

public static Logger getSizeLogger(String file, String fileSize) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender fileAppender = new RollingFileAppender();
    fileAppender.setContext(loggerContext);
    fileAppender.setFile(file);
    fileAppender.setAppend(true);
    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setFileNamePattern(file + "%i");
    rollingPolicy.setParent(fileAppender);
    rollingPolicy.start();
    rollingPolicy.setMaxIndex(100);
    fileAppender.setRollingPolicy(rollingPolicy);
    SizeBasedTriggeringPolicy triggeringPolicy = new SizeBasedTriggeringPolicy();
    triggeringPolicy.setContext(loggerContext);
    triggeringPolicy.setMaxFileSize(fileSize);
    triggeringPolicy.start();
    fileAppender.setTriggeringPolicy(triggeringPolicy);
    PatternLayoutEncoder layoutEncoder = new PatternLayoutEncoder();
    layoutEncoder.setContext(loggerContext);
    layoutEncoder.setPattern("[%d  %-5p %c{1}] %msg%n");
    layoutEncoder.start();
    fileAppender.setEncoder(layoutEncoder);
    fileAppender.start();
    // configures  logger
    Logger rootLogger = loggerContext.getLogger(BaseTailerTest.class.getName() + "size");
    rootLogger.setLevel(Level.DEBUG);
    rootLogger.addAppender(fileAppender);
    return rootLogger;
}
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) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 2 with SizeBasedTriggeringPolicy

use of ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy in project sonarqube by SonarSource.

the class LogbackHelperTest method createRollingPolicy_size.

@Test
public void createRollingPolicy_size() {
    props.set("sonar.log.rollingPolicy", "size:1MB");
    props.set("sonar.log.maxFiles", "20");
    LoggerContext ctx = underTest.getRootContext();
    LogbackHelper.RollingPolicy policy = underTest.createRollingPolicy(ctx, props, "sonar");
    Appender appender = policy.createAppender("SONAR_FILE");
    assertThat(appender).isInstanceOf(RollingFileAppender.class);
    // max 20 files of 1Mb
    RollingFileAppender fileAppender = (RollingFileAppender) appender;
    FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) fileAppender.getRollingPolicy();
    assertThat(rollingPolicy.getMaxIndex()).isEqualTo(20);
    assertThat(rollingPolicy.getFileNamePattern()).endsWith("sonar.%i.log");
    SizeBasedTriggeringPolicy triggeringPolicy = (SizeBasedTriggeringPolicy) fileAppender.getTriggeringPolicy();
    assertThat(triggeringPolicy.getMaxFileSize()).isEqualTo("1MB");
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) 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) Test(org.junit.Test)

Example 3 with SizeBasedTriggeringPolicy

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

the class LogWriter method createAppender.

public Appender<ILoggingEvent> createAppender(final Context context, final Encoder<ILoggingEvent> encoder) {
    SlingContextUtil ctxUtil = new SlingContextUtil(context, this);
    OutputStreamAppender<ILoggingEvent> appender;
    if (FILE_NAME_CONSOLE.equals(fileName)) {
        appender = new ConsoleAppender<ILoggingEvent>();
        appender.setName(FILE_NAME_CONSOLE);
    } else {
        ctxUtil.addInfo("Configuring appender " + getFileName());
        SlingRollingFileAppender<ILoggingEvent> rollingAppender = new SlingRollingFileAppender<ILoggingEvent>();
        rollingAppender.setAppend(true);
        rollingAppender.setFile(getFileName());
        Matcher sizeMatcher = SIZE_SPEC.matcher(getLogRotation());
        if (sizeMatcher.matches()) {
            // group 1 is the base size and is an integer number
            final long baseSize = Long.parseLong(sizeMatcher.group(1));
            // this will take the final size value
            final long maxSize;
            // group 2 is optional and is the size spec. If not null it is
            // at least one character long and the first character is enough
            // for use to know (the second is of no use here)
            final String factorString = sizeMatcher.group(2);
            if (factorString == null) {
                // no factor define, hence no multiplication
                maxSize = baseSize;
            } else {
                switch(factorString.charAt(0)) {
                    case 'k':
                    case 'K':
                        maxSize = baseSize * FACTOR_KB;
                        break;
                    case 'm':
                    case 'M':
                        maxSize = baseSize * FACTOR_MB;
                        break;
                    case 'g':
                    case 'G':
                        maxSize = baseSize * FACTOR_GB;
                        break;
                    default:
                        // we don't really expect this according to the
                        // pattern
                        maxSize = baseSize;
                }
            }
            SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
            triggeringPolicy.setMaxFileSize(String.valueOf(maxSize));
            triggeringPolicy.setContext(context);
            triggeringPolicy.start();
            rollingAppender.setTriggeringPolicy(triggeringPolicy);
            FixedWindowRollingPolicy pol = new FixedWindowRollingPolicy();
            pol.setMinIndex(1);
            pol.setMaxIndex(getLogNumber());
            pol.setFileNamePattern(getFileName() + "%i");
            pol.setContext(context);
            pol.setParent(rollingAppender);
            pol.start();
            rollingAppender.setRollingPolicy(pol);
        } else {
            TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
            String fileNamePattern = createFileNamePattern(getFileName(), getLogRotation());
            policy.setFileNamePattern(fileNamePattern);
            policy.setMaxHistory(getLogNumber());
            policy.setContext(context);
            policy.setParent(rollingAppender);
            policy.start();
            rollingAppender.setTriggeringPolicy(policy);
            ctxUtil.addInfo("Configured TimeBasedRollingPolicy with pattern " + fileNamePattern);
        }
        rollingAppender.setLogWriter(this);
        rollingAppender.setName(getAppenderName());
        appender = rollingAppender;
    }
    if (bufferedLogging && encoder instanceof LayoutWrappingEncoder) {
        ((LayoutWrappingEncoder) encoder).setImmediateFlush(false);
        ctxUtil.addInfo("Setting immediateFlush to false");
    } else {
        ctxUtil.addInfo("immediateFlush property not modified. Defaults to true");
    }
    appender.setContext(context);
    appender.setEncoder(encoder);
    appender.start();
    ctxUtil.addInfo("Completed configuring appender with name " + getFileName());
    return appender;
}
Also used : FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) Matcher(java.util.regex.Matcher) SlingContextUtil(org.apache.sling.commons.log.logback.internal.util.SlingContextUtil) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) SlingRollingFileAppender(org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) LayoutWrappingEncoder(ch.qos.logback.core.encoder.LayoutWrappingEncoder) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 4 with SizeBasedTriggeringPolicy

use of ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy 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)

Example 5 with SizeBasedTriggeringPolicy

use of ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy 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)

Aggregations

SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)8 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)7 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)5 LoggerContext (ch.qos.logback.classic.LoggerContext)4 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)3 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)3 FileAppender (ch.qos.logback.core.FileAppender)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 DefaultTimeBasedFileNamingAndTriggeringPolicy (ch.qos.logback.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy)1 SizeAndTimeBasedRollingPolicy (ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy)1 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)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