Search in sources :

Example 61 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 62 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 63 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)

Example 64 with Appender

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

use of ch.qos.logback.core.Appender in project ddf by codice.

the class ApplicationConfigInstallerTest method testRunASE.

/**
     * Tests the {@link ApplicationConfigInstaller#run()} method for the case
     * where an ApplicationServiceException is thrown by appService.addApplication(..)
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testRunASE() throws Exception {
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    final Appender mockAppender = mock(Appender.class);
    when(mockAppender.getName()).thenReturn("MOCK");
    root.addAppender(mockAppender);
    FeaturesService featuresService = mock(FeaturesService.class);
    ApplicationService testAppService = mock(ApplicationServiceImpl.class);
    doThrow(new ApplicationServiceException()).when(testAppService).startApplication(anyString());
    URL fileURL = this.getClass().getResource("/" + GOOD_FILE);
    ApplicationConfigInstaller configInstaller = getApplicationConfigInstaller(fileURL.getFile(), testAppService, featuresService, START_FEATURE, STOP_FEATURE);
    configInstaller.run();
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

        @Override
        public boolean matches(final Object argument) {
            return ((LoggingEvent) argument).getFormattedMessage().contains(RUN_ASE_EX);
        }
    }));
}
Also used : Appender(ch.qos.logback.core.Appender) Logger(org.slf4j.Logger) URL(java.net.URL) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) ArgumentMatcher(org.mockito.ArgumentMatcher) FeaturesService(org.apache.karaf.features.FeaturesService) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Aggregations

Appender (ch.qos.logback.core.Appender)118 Test (org.junit.Test)62 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)51 Logger (ch.qos.logback.classic.Logger)32 LoggerContext (ch.qos.logback.classic.LoggerContext)32 FileAppender (ch.qos.logback.core.FileAppender)27 Logger (org.slf4j.Logger)25 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)21 ArgumentMatcher (org.mockito.ArgumentMatcher)19 AsyncAppender (ch.qos.logback.classic.AsyncAppender)18 File (java.io.File)14 OnMarkerEvaluator (ch.qos.logback.classic.boolex.OnMarkerEvaluator)13 EvaluatorFilter (ch.qos.logback.core.filter.EvaluatorFilter)13 LogstashTcpSocketAppender (net.logstash.logback.appender.LogstashTcpSocketAppender)13 ArrayList (java.util.ArrayList)11 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)11 OutputStreamAppender (ch.qos.logback.core.OutputStreamAppender)10 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)9 DBAppender (ch.qos.logback.classic.db.DBAppender)8 Test (org.junit.jupiter.api.Test)7