Search in sources :

Example 86 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project graylog2-server by Graylog2.

the class LoggersResource method getAppender.

private Appender getAppender(final String appenderName) {
    final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    final Configuration configuration = loggerContext.getConfiguration();
    return configuration.getAppender(appenderName);
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 87 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project spring-boot by spring-projects.

the class Log4J2LoggingSystem method beforeInitialize.

@Override
public void beforeInitialize() {
    LoggerContext loggerContext = getLoggerContext();
    if (isAlreadyInitialized(loggerContext)) {
        return;
    }
    super.beforeInitialize();
    loggerContext.getConfiguration().addFilter(FILTER);
}
Also used : LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 88 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project spring-boot by spring-projects.

the class Log4J2LoggingSystem method initialize.

@Override
public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) {
    LoggerContext loggerContext = getLoggerContext();
    if (isAlreadyInitialized(loggerContext)) {
        return;
    }
    loggerContext.getConfiguration().removeFilter(FILTER);
    super.initialize(initializationContext, configLocation, logFile);
    markAsInitialized(loggerContext);
}
Also used : LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 89 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project spring-boot by spring-projects.

the class Log4J2LoggingSystemTests method initializationIsOnlyPerformedOnceUntilCleanedUp.

@Test
public void initializationIsOnlyPerformedOnceUntilCleanedUp() throws Exception {
    LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    PropertyChangeListener listener = mock(PropertyChangeListener.class);
    loggerContext.addPropertyChangeListener(listener);
    this.loggingSystem.beforeInitialize();
    this.loggingSystem.initialize(null, null, null);
    this.loggingSystem.beforeInitialize();
    this.loggingSystem.initialize(null, null, null);
    this.loggingSystem.beforeInitialize();
    this.loggingSystem.initialize(null, null, null);
    verify(listener, times(2)).propertyChange(any(PropertyChangeEvent.class));
    this.loggingSystem.cleanUp();
    this.loggingSystem.beforeInitialize();
    this.loggingSystem.initialize(null, null, null);
    verify(listener, times(4)).propertyChange(any(PropertyChangeEvent.class));
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 90 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project camel by apache.

the class ConsumingAppender method newAppender.

public static Appender newAppender(String loggerName, String appenderName, String patter, Level level, Consumer<LogEvent> consumer) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    config.removeLogger(loggerName);
    ConsumingAppender appender = new ConsumingAppender(appenderName, patter, consumer);
    appender.start();
    LoggerConfig loggerConfig = LoggerConfig.createLogger(true, level, loggerName, "true", new AppenderRef[] { AppenderRef.createAppenderRef(appenderName, null, null) }, null, config, null);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger(loggerName, loggerConfig);
    ctx.updateLoggers();
    return appender;
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

LoggerContext (org.apache.logging.log4j.core.LoggerContext)163 Configuration (org.apache.logging.log4j.core.config.Configuration)57 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)36 Test (org.junit.Test)33 Appender (org.apache.logging.log4j.core.Appender)18 File (java.io.File)12 IOException (java.io.IOException)12 Logger (org.apache.logging.log4j.Logger)11 BeforeClass (org.junit.BeforeClass)11 Map (java.util.Map)10 Level (org.apache.logging.log4j.Level)8 LogEvent (org.apache.logging.log4j.core.LogEvent)8 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)7 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)7 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)7 Logger (org.apache.logging.log4j.core.Logger)6 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)5 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)4 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4