void register() { if (LogManager.getContext(false) instanceof LoggerContext) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); if (ctx.getConfiguration() instanceof AbstractConfiguration) { final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); final Appender appender = getSingleton(); appender.start(); config.addAppender(appender); final Logger rootLogger = LogManager.getRootLogger(); final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName()); loggerConfig.addAppender(appender, null, null); ctx.updateLoggers(); } } }
void deregister() { if (LogManager.getContext(false) instanceof LoggerContext) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); if (ctx.getConfiguration() instanceof AbstractConfiguration) { final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); final Appender appender = getSingleton(); appender.stop(); config.removeAppender(appender.getName()); final Logger rootLogger = LogManager.getRootLogger(); final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName()); loggerConfig.removeAppender(appender.getName()); ctx.updateLoggers(); } } }
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); } } } }
/** * 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 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(); } }
/** * 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(); } }
void register() { if (LogManager.getContext(false) instanceof LoggerContext) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); if (ctx.getConfiguration() instanceof AbstractConfiguration) { final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); final Appender appender = getSingleton(); appender.start(); config.addAppender(appender); final Logger rootLogger = LogManager.getRootLogger(); final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName()); loggerConfig.addAppender(appender, null, null); ctx.updateLoggers(); } } }
void deregister() { if (LogManager.getContext(false) instanceof LoggerContext) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); if (ctx.getConfiguration() instanceof AbstractConfiguration) { final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); final Appender appender = getSingleton(); appender.stop(); config.removeAppender(appender.getName()); final Logger rootLogger = LogManager.getRootLogger(); final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName()); loggerConfig.removeAppender(appender.getName()); ctx.updateLoggers(); } } }