Search in sources :

Example 26 with Appender

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

the class CeProcessLoggingTest method do_not_log_to_console.

@Test
public void do_not_log_to_console() {
    LoggerContext ctx = underTest.configure(props);
    Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
    Appender appender = root.getAppender("CONSOLE");
    assertThat(appender).isNull();
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Example 27 with Appender

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

the class LogbackHelperTest method createRollingPolicy_none.

@Test
public void createRollingPolicy_none() {
    props.set("sonar.log.rollingPolicy", "none");
    LoggerContext ctx = underTest.getRootContext();
    LogbackHelper.RollingPolicy policy = underTest.createRollingPolicy(ctx, props, "sonar");
    Appender appender = policy.createAppender("SONAR_FILE");
    assertThat(appender).isNotInstanceOf(RollingFileAppender.class).isInstanceOf(FileAppender.class);
}
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) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Example 28 with Appender

use of ch.qos.logback.core.Appender 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 29 with Appender

use of ch.qos.logback.core.Appender in project jackrabbit-oak by apache.

the class LogLevelModifier method finished.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void finished(Description description) {
    for (Iterator<AppenderFilter> it = appenderFilters.iterator(); it.hasNext(); ) {
        AppenderFilter appenderFilter = (AppenderFilter) it.next();
        appenderFilter.finished();
    }
    for (Iterator<LoggerLevel> it = loggerLevels.iterator(); it.hasNext(); ) {
        LoggerLevel loggerLevel = (LoggerLevel) it.next();
        loggerLevel.finished();
    }
    for (Iterator<Appender> it = newAppenders.iterator(); it.hasNext(); ) {
        Appender appender = it.next();
        rootLogger().detachAppender(appender);
    }
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Appender(ch.qos.logback.core.Appender)

Example 30 with Appender

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

the class LogConfigManager method onResetComplete.

@SuppressWarnings("unchecked")
@Override
public void onResetComplete(LoggerContext context) {
    //The OSGi config based appenders are attached on reset complete as by that time Logback config
    // would have been parsed and various appenders and logger configured. Now we use the OSGi config
    // 1. If an appender with same name as one defined by OSGi config is found then it takes precedence
    // 2. If no existing appender is found then we create one use that
    Map<String, Appender<ILoggingEvent>> appendersByName = new HashMap<String, Appender<ILoggingEvent>>();
    Map<String, Appender<ILoggingEvent>> configuredAppenders = (Map<String, Appender<ILoggingEvent>>) context.getObject(ActionConst.APPENDER_BAG);
    if (configuredAppenders == null) {
        configuredAppenders = Collections.emptyMap();
    }
    Map<Appender, LoggerSpecificEncoder> encoders = new HashMap<Appender, LoggerSpecificEncoder>();
    for (LogConfig config : getLogConfigs()) {
        Appender<ILoggingEvent> appender = null;
        if (config.isAppenderDefined()) {
            LogWriter lw = config.getLogWriter();
            final String appenderName = lw.getAppenderName();
            appender = appendersByName.get(appenderName);
            if (appender == null) {
                appender = configuredAppenders.get(appenderName);
                if (appender != null) {
                    contextUtil.addInfo("Found overriding configuration for appender " + appenderName + " in Logback config. OSGi config would be ignored");
                }
            }
            if (appender == null) {
                LoggerSpecificEncoder encoder = new LoggerSpecificEncoder(getDefaultLayout());
                appender = lw.createAppender(loggerContext, encoder);
                encoders.put(appender, encoder);
                appendersByName.put(appenderName, appender);
            }
            if (encoders.containsKey(appender)) {
                encoders.get(appender).addLogConfig(config);
            }
        }
        for (String category : config.getCategories()) {
            ch.qos.logback.classic.Logger logger = loggerContext.getLogger(category);
            if (config.isResetToDefault()) {
                logger.setLevel(null);
                //Reset additivity
                logger.setAdditive(true);
            } else {
                logger.setLevel(config.getLogLevel());
                if (appender != null) {
                    logger.setAdditive(config.isAdditive());
                    logger.addAppender(appender);
                    contextUtil.addInfo("Registering appender " + appender.getName() + "(" + appender.getClass() + ") with logger " + logger.getName());
                }
            }
        }
    }
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) OutputStreamAppender(ch.qos.logback.core.OutputStreamAppender) Appender(ch.qos.logback.core.Appender) LoggerSpecificEncoder(org.apache.sling.commons.log.logback.internal.util.LoggerSpecificEncoder) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Appender (ch.qos.logback.core.Appender)50 Test (org.junit.Test)36 Logger (org.slf4j.Logger)26 ArgumentMatcher (org.mockito.ArgumentMatcher)23 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)15 Logger (ch.qos.logback.classic.Logger)14 FileAppender (ch.qos.logback.core.FileAppender)14 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)13 LoggerContext (ch.qos.logback.classic.LoggerContext)12 FeaturesService (org.apache.karaf.features.FeaturesService)10 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)9 HashSet (java.util.HashSet)9 Repository (org.apache.karaf.features.Repository)9 File (java.io.File)8 ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)8 ArrayList (java.util.ArrayList)7 Application (org.codice.ddf.admin.application.service.Application)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 AsyncAppender (ch.qos.logback.classic.AsyncAppender)2