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());
}
}
}
}
}
Aggregations