Search in sources :

Example 1 with SlingRollingFileAppender

use of org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender 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 2 with SlingRollingFileAppender

use of org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender in project sling by apache.

the class LogbackManager method determineLoggerState.

// ~ ----------------------------------------------WebConsole Support
public LoggerStateContext determineLoggerState() {
    final List<Logger> loggers = getLoggerContext().getLoggerList();
    final LoggerStateContext ctx = new LoggerStateContext(loggers);
    //2. Other means - Configured via Logback config or any other means
    for (LogConfig lc : logConfigManager.getLogConfigs()) {
        for (String category : lc.getCategories()) {
            ctx.osgiConfiguredLoggers.put(category, lc);
        }
    }
    for (Logger logger : loggers) {
        boolean hasOnlySlingRollingAppenders = true;
        Iterator<Appender<ILoggingEvent>> itr = logger.iteratorForAppenders();
        while (itr.hasNext()) {
            Appender<ILoggingEvent> a = itr.next();
            if (a.getName() != null && !ctx.appenders.containsKey(a.getName())) {
                ctx.appenders.put(a.getName(), a);
            }
            if (!(a instanceof SlingRollingFileAppender)) {
                hasOnlySlingRollingAppenders = false;
            }
        }
        if (logger.getLevel() == null) {
            continue;
        }
        boolean configuredViaOSGiConfig = ctx.osgiConfiguredLoggers.containsKey(logger.getName());
        if (!configuredViaOSGiConfig || (configuredViaOSGiConfig && !hasOnlySlingRollingAppenders)) {
            ctx.nonOSgiConfiguredLoggers.add(logger);
        }
    }
    return ctx;
}
Also used : Appender(ch.qos.logback.core.Appender) SlingRollingFileAppender(org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender) SlingRollingFileAppender(org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent)

Example 3 with SlingRollingFileAppender

use of org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender 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 4 with SlingRollingFileAppender

use of org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender 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

ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)4 SlingRollingFileAppender (org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender)4 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)2 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)2 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)2 Test (org.junit.Test)2 Logger (ch.qos.logback.classic.Logger)1 Appender (ch.qos.logback.core.Appender)1 LayoutWrappingEncoder (ch.qos.logback.core.encoder.LayoutWrappingEncoder)1 Matcher (java.util.regex.Matcher)1 SlingContextUtil (org.apache.sling.commons.log.logback.internal.util.SlingContextUtil)1