private void setParents() { for (final Map.Entry<String, LoggerConfig> entry : loggerConfigs.entrySet()) { final LoggerConfig logger = entry.getValue(); String key = entry.getKey(); if (!key.isEmpty()) { final int i = key.lastIndexOf('.'); if (i > 0) { key = key.substring(0, i); LoggerConfig parent = getLoggerConfig(key); if (parent == null) { parent = root; } logger.setParent(parent); } else { logger.setParent(root); } } } }
public static void setLevel(Logger logger, Level level) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Configuration config = ctx.getConfiguration(); LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName()); LoggerConfig specificConfig = loggerConfig; // We need a specific configuration for this logger, // otherwise we would change the level of all other loggers // having the original configuration as parent as well if (!loggerConfig.getName().equals(logger.getName())) { specificConfig = new LoggerConfig(logger.getName(), level, true); specificConfig.setParent(loggerConfig); config.addLogger(logger.getName(), specificConfig); } specificConfig.setLevel(level); ctx.updateLoggers(); }
/** * Marks a LoggerConfig as additive. This method is synchronized in case a Logger with the same name is being * updated at the same time. * * Note: This method is not used when configuring via configuration. It is primarily used by unit tests. * * @param logger The Logger the Appender will be associated with. * @param additive True if the LoggerConfig should be additive, false otherwise. */ @Override public synchronized void setLoggerAdditive(final org.apache.logging.log4j.core.Logger logger, final boolean additive) { final String loggerName = logger.getName(); final LoggerConfig lc = getLoggerConfig(loggerName); if (lc.getName().equals(loggerName)) { lc.setAdditive(additive); } else { final LoggerConfig nlc = new LoggerConfig(loggerName, lc.getLevel(), additive); nlc.setParent(lc); loggerConfigs.putIfAbsent(loggerName, nlc); setParents(); logger.getContext().updateLoggers(); } }
/** * Associates a Filter with a LoggerConfig. This method is synchronized in case a Logger with the same name is being * updated at the same time. * * Note: This method is not used when configuring via configuration. It is primarily used by unit tests. * * @param logger The Logger the Footer will be associated with. * @param filter The Filter. */ @Override public synchronized void addLoggerFilter(final org.apache.logging.log4j.core.Logger logger, final Filter filter) { final String loggerName = logger.getName(); final LoggerConfig lc = getLoggerConfig(loggerName); if (lc.getName().equals(loggerName)) { lc.addFilter(filter); } else { final LoggerConfig nlc = new LoggerConfig(loggerName, lc.getLevel(), lc.isAdditive()); nlc.addFilter(filter); nlc.setParent(lc); loggerConfigs.putIfAbsent(loggerName, nlc); setParents(); logger.getContext().updateLoggers(); } }
/** * Associates an Appender with a LoggerConfig. This method is synchronized in case a Logger with the same name is * being updated at the same time. * * Note: This method is not used when configuring via configuration. It is primarily used by unit tests. * * @param logger The Logger the Appender will be associated with. * @param appender The Appender. */ @Override public synchronized void addLoggerAppender(final org.apache.logging.log4j.core.Logger logger, final Appender appender) { final String loggerName = logger.getName(); appenders.putIfAbsent(appender.getName(), appender); final LoggerConfig lc = getLoggerConfig(loggerName); if (lc.getName().equals(loggerName)) { lc.addAppender(appender, null, null); } else { final LoggerConfig nlc = new LoggerConfig(loggerName, lc.getLevel(), lc.isAdditive()); nlc.addAppender(appender, null, null); nlc.setParent(lc); loggerConfigs.putIfAbsent(loggerName, nlc); setParents(); logger.getContext().updateLoggers(); } }