Search in sources :

Example 1 with LoggerSpecificEncoder

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

ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 Appender (ch.qos.logback.core.Appender)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 OutputStreamAppender (ch.qos.logback.core.OutputStreamAppender)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 LoggerSpecificEncoder (org.apache.sling.commons.log.logback.internal.util.LoggerSpecificEncoder)1