Search in sources :

Example 51 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project geode by apache.

the class AlertAppender method removeAlertListener.

public synchronized boolean removeAlertListener(final DistributedMember member) {
    final boolean memberWasFound = this.listeners.remove(new Listener(null, member));
    if (memberWasFound) {
        if (this.listeners.size() == 0) {
            this.appenderContext.getLoggerContext().removePropertyChangeListener(this);
            this.appenderContext.getLoggerConfig().removeAppender(APPENDER_NAME);
        } else {
            LoggerConfig loggerConfig = this.appenderContext.getLoggerConfig();
            loggerConfig.addAppender(this, this.listeners.get(0).getLevel(), null);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Removed alert listener for member {}", member);
        }
    }
    return memberWasFound;
}
Also used : PropertyChangeListener(java.beans.PropertyChangeListener) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 52 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project geode by apache.

the class DUnitLauncher method addSuspectFileAppender.

/**
   * Add an appender to Log4j which sends all INFO+ messages to a separate file which will be used
   * later to scan for suspect strings. The pattern of the messages conforms to the original log
   * format so that hydra will be able to parse them.
   */
private static void addSuspectFileAppender(final String workspaceDir) {
    final String suspectFilename = new File(workspaceDir, SUSPECT_FILENAME).getAbsolutePath();
    final LoggerContext appenderContext = ((org.apache.logging.log4j.core.Logger) LogManager.getLogger(LogService.BASE_LOGGER_NAME)).getContext();
    final PatternLayout layout = PatternLayout.createLayout("[%level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} <%thread> tid=%tid] %message%n%throwable%n", null, null, null, Charset.defaultCharset(), true, false, "", "");
    final FileAppender fileAppender = FileAppender.createAppender(suspectFilename, "true", "false", DUnitLauncher.class.getName(), "true", "false", "false", "0", layout, null, null, null, appenderContext.getConfiguration());
    fileAppender.start();
    LoggerConfig loggerConfig = appenderContext.getConfiguration().getLoggerConfig(LogService.BASE_LOGGER_NAME);
    loggerConfig.addAppender(fileAppender, Level.INFO, null);
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) File(java.io.File) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 53 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project geode by apache.

the class AlertAppenderJUnitTest method testAppenderToConfigHandling.

/**
   * Verifies that the appender is correctly added and removed from the Log4j configuration and that
   * when the configuration is changed the appender is still there.
   */
@Test
public void testAppenderToConfigHandling() throws Exception {
    LogService.setBaseLogLevel(Level.WARN);
    final String appenderName = AlertAppender.getInstance().getName();
    final AppenderContext appenderContext = LogService.getAppenderContext();
    LoggerConfig loggerConfig = appenderContext.getLoggerConfig();
    // Find out home many appenders exist before we get started
    final int startingSize = loggerConfig.getAppenders().size();
    // Add a listener and verify that the appender was added to log4j
    DistributedMember member1 = createTestDistributedMember("Member1");
    AlertAppender.getInstance().addAlertListener(member1, Alert.SEVERE);
    assertEquals(loggerConfig.getAppenders().values().toString(), startingSize + 1, loggerConfig.getAppenders().size());
    assertTrue(loggerConfig.getAppenders().containsKey(appenderName));
    // Add another listener and verify that there's still only 1 alert appender
    DistributedMember member2 = createTestDistributedMember("Member1");
    AlertAppender.getInstance().addAlertListener(member2, Alert.SEVERE);
    assertEquals(startingSize + 1, loggerConfig.getAppenders().size());
    // Modify the config and verify that the appender still exists
    assertEquals(Level.WARN, LogService.getLogger(LogService.BASE_LOGGER_NAME).getLevel());
    LogService.setBaseLogLevel(Level.INFO);
    assertEquals(Level.INFO, LogService.getLogger(LogService.BASE_LOGGER_NAME).getLevel());
    loggerConfig = appenderContext.getLoggerConfig();
    assertEquals(startingSize + 1, loggerConfig.getAppenders().size());
    assertTrue(loggerConfig.getAppenders().containsKey(appenderName));
    // Remove the listeners and verify that the appender was removed from log4j
    assertTrue(AlertAppender.getInstance().removeAlertListener(member2));
    assertFalse(AlertAppender.getInstance().removeAlertListener(member1));
    assertEquals(startingSize, loggerConfig.getAppenders().size());
    assertFalse(loggerConfig.getAppenders().containsKey(appenderName));
}
Also used : DistributedMember(org.apache.geode.distributed.DistributedMember) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 54 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project logging-log4j2 by apache.

the class Logger method getParent.

/**
     * This method is only used for 1.x compatibility. Returns the parent of this Logger. If it doesn't already exist
     * return a temporary Logger.
     *
     * @return The parent Logger.
     */
public Logger getParent() {
    final LoggerConfig lc = privateConfig.loggerConfig.getName().equals(getName()) ? privateConfig.loggerConfig.getParent() : privateConfig.loggerConfig;
    if (lc == null) {
        return null;
    }
    final String lcName = lc.getName();
    final MessageFactory messageFactory = getMessageFactory();
    if (context.hasLogger(lcName, messageFactory)) {
        return context.getLogger(lcName, messageFactory);
    }
    return new Logger(context, lcName, messageFactory);
}
Also used : MessageFactory(org.apache.logging.log4j.message.MessageFactory) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 55 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project logging-log4j2 by apache.

the class Log4j1ConfigurationFactoryTest method testConsole.

private Layout<?> testConsole(final String configResource) throws Exception {
    final Configuration configuration = getConfiguration(configResource);
    final String name = "Console";
    final ConsoleAppender appender = configuration.getAppender(name);
    assertNotNull("Missing appender '" + name + "' in configuration " + configResource + " → " + configuration, appender);
    assertEquals(Target.SYSTEM_ERR, appender.getTarget());
    //
    final LoggerConfig loggerConfig = configuration.getLoggerConfig("com.example.foo");
    assertNotNull(loggerConfig);
    assertEquals(Level.DEBUG, loggerConfig.getLevel());
    configuration.start();
    configuration.stop();
    return appender.getLayout();
}
Also used : ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)63 Configuration (org.apache.logging.log4j.core.config.Configuration)38 LoggerContext (org.apache.logging.log4j.core.LoggerContext)32 Appender (org.apache.logging.log4j.core.Appender)11 Level (org.apache.logging.log4j.Level)9 Test (org.junit.Test)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)6 Logger (org.apache.logging.log4j.Logger)5 Filter (org.apache.logging.log4j.core.Filter)5 Configuration.getConfiguration (javax.security.auth.login.Configuration.getConfiguration)4 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 ThresholdFilter (org.apache.logging.log4j.core.filter.ThresholdFilter)4 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)3 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)3 PropertyChangeListener (java.beans.PropertyChangeListener)2 File (java.io.File)2 Path (java.nio.file.Path)2 Map (java.util.Map)2 AppenderContext (org.apache.geode.internal.logging.log4j.AppenderContext)2