Search in sources :

Example 31 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project hive by apache.

the class TestOperationLoggingLayout method checkAppenderState.

/**
 * assert that the appender for the given queryId is in the expected state.
 * @param msg a diagnostic
 * @param routingAppenderName name of the RoutingAppender
 * @param queryId the query id to use as a key
 * @param expectedStopped the expected stop state
 */
private void checkAppenderState(String msg, String routingAppenderName, String queryId, boolean expectedStopped) throws NoSuchFieldException, IllegalAccessException {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = context.getConfiguration();
    LoggerConfig loggerConfig = configuration.getRootLogger();
    Map<String, Appender> appendersMap = loggerConfig.getAppenders();
    RoutingAppender routingAppender = (RoutingAppender) appendersMap.get(routingAppenderName);
    Assert.assertNotNull(msg + "could not find routingAppender " + routingAppenderName, routingAppender);
    Field defaultsField = RoutingAppender.class.getDeclaredField("appenders");
    defaultsField.setAccessible(true);
    ConcurrentHashMap appenders = (ConcurrentHashMap) defaultsField.get(routingAppender);
    AppenderControl appenderControl = (AppenderControl) appenders.get(queryId);
    if (!expectedStopped) {
        Assert.assertNotNull(msg + "Could not find AppenderControl for query id " + queryId, appenderControl);
        Appender appender = appenderControl.getAppender();
        Assert.assertNotNull(msg + "could not find Appender for query id " + queryId + " from AppenderControl " + appenderControl, appender);
        Assert.assertEquals(msg + "Appender for query is in unexpected state", expectedStopped, appender.isStopped());
    } else {
        Assert.assertNull(msg + "AppenderControl for query id is not removed" + queryId, appenderControl);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) LogDivertAppender(org.apache.hadoop.hive.ql.log.LogDivertAppender) RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) HushableRandomAccessFileAppender(org.apache.hadoop.hive.ql.log.HushableRandomAccessFileAppender) Field(java.lang.reflect.Field) RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 32 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project herd by FINRAOS.

the class Log4j2LoggingHelper method executeWithoutLogging.

@Override
public void executeWithoutLogging(List<Class<?>> loggingClasses, Command command) throws Exception {
    // Get the logger context and it's associated configuration.
    LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = loggerContext.getConfiguration();
    // Temporarily turn off logging.
    Map<LoggerConfig, Level> loggerToOriginalLevel = new HashMap<>();
    for (Class<?> loggingClass : loggingClasses) {
        LoggerConfig loggerConfig = loggingClass == null ? null : configuration.getLoggerConfig(loggingClass.getName());
        Level originalLevel = loggerConfig == null ? null : loggerConfig.getLevel();
        if (loggerConfig != null) {
            loggerConfig.setLevel(Level.OFF);
            loggerToOriginalLevel.put(loggerConfig, originalLevel);
        }
    }
    // This causes all Loggers to re-fetch information from their LoggerConfig.
    loggerContext.updateLoggers();
    try {
        // Execute the command.
        command.execute();
    } finally {
        for (Map.Entry<LoggerConfig, Level> entry : loggerToOriginalLevel.entrySet()) {
            LoggerConfig loggerConfig = entry.getKey();
            Level originalLevel = entry.getValue();
            // Turn the original logging back on.
            loggerConfig.setLevel(originalLevel);
        }
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) HashMap(java.util.HashMap) Level(org.apache.logging.log4j.Level) LoggerContext(org.apache.logging.log4j.core.LoggerContext) HashMap(java.util.HashMap) Map(java.util.Map) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 33 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project herd by FINRAOS.

the class Log4j2LoggingHelper method getLogLevel.

@Override
public LogLevel getLogLevel(String loggerName) {
    // Get the main logger context.
    LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    // Get the main configuration.
    Configuration configuration = loggerContext.getConfiguration();
    // Get the logging configuration for the specified logger name from the main configuration.
    LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName);
    // Return the logging level for the logging configuration.
    return log4jToGenericLogLevel(loggerConfig.getLevel());
}
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)

Example 34 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project herd by FINRAOS.

the class Log4j2LoggingHelper method removeLoggingAppender.

@Override
public void removeLoggingAppender(String appenderName) {
    // Get the configuration and remove the appender from it.
    final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    final Configuration configuration = loggerContext.getConfiguration();
    configuration.getRootLogger().removeAppender(appenderName);
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 35 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project jmeter-plugins-manager by undera.

the class LoggingConfigurator method configure.

public void configure() {
    PatternLayout.Builder patternBuilder = PatternLayout.newBuilder();
    patternBuilder.withPattern("%d %p %c{1.}: %m%n");
    PatternLayout layout = patternBuilder.build();
    ConsoleAppender consoleAppender = ConsoleAppender.createDefaultAppenderForLayout(layout);
    consoleAppender.start();
    Configuration configuration = ((LoggerContext) LogManager.getContext(false)).getConfiguration();
    LoggerConfig rootLogger = configuration.getRootLogger();
    rootLogger.setLevel(Level.INFO);
    rootLogger.addAppender(consoleAppender, Level.INFO, null);
}
Also used : ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)210 LoggerContext (org.apache.logging.log4j.core.LoggerContext)120 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)71 Test (org.junit.Test)54 Appender (org.apache.logging.log4j.core.Appender)50 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)37 Test (org.junit.jupiter.api.Test)29 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)22 File (java.io.File)21 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)20 Path (java.nio.file.Path)14 Map (java.util.Map)14 ArrayList (java.util.ArrayList)13 Level (org.apache.logging.log4j.Level)13 ListAppender (org.apache.log4j.ListAppender)12 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)12 ConfigurationSource (org.apache.logging.log4j.core.config.ConfigurationSource)12 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)11 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)11 InputStream (java.io.InputStream)10