Search in sources :

Example 1 with LoggerContext

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

the class LogVerifier method newAppender.

private Appender newAppender() {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    LogAppender appender = new LogAppender("cdi-rule");
    appender.start();
    config.addAppender(appender);
    config.getRootLogger().removeAppender("cdi-rule");
    config.getRootLogger().addAppender(appender, Level.TRACE, null);
    ctx.updateLoggers();
    return appender;
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 2 with LoggerContext

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

the class LogBodyWithNewLineTest method setUp.

public void setUp() throws Exception {
    super.setUp();
    writer = new StringWriter();
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    Appender appender = WriterAppender.newBuilder().setLayout(PatternLayout.newBuilder().withPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN).build()).setTarget(writer).setName("Writer").build();
    appender.start();
    config.addAppender(appender);
    config.getRootLogger().removeAppender("Writer");
    config.getRootLogger().addAppender(appender, Level.INFO, null);
    ctx.updateLoggers();
}
Also used : WriterAppender(org.apache.logging.log4j.core.appender.WriterAppender) Appender(org.apache.logging.log4j.core.Appender) StringWriter(java.io.StringWriter) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 3 with LoggerContext

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

the class TestHive method testMetaStoreApiTiming.

/**
   * Test logging of timing for metastore api calls
   *
   * @throws Throwable
   */
public void testMetaStoreApiTiming() throws Throwable {
    // Get the RootLogger which, if you don't have log4j2-test.properties defined, will only log ERRORs
    Logger logger = LogManager.getLogger("hive.ql.metadata.Hive");
    Level oldLevel = logger.getLevel();
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
    loggerConfig.setLevel(Level.DEBUG);
    ctx.updateLoggers();
    // Create a String Appender to capture log output
    StringAppender appender = StringAppender.createStringAppender("%m");
    appender.addToLogger(logger.getName(), Level.DEBUG);
    appender.start();
    try {
        hm.clearMetaCallTiming();
        hm.getAllDatabases();
        hm.dumpAndClearMetaCallTiming("test");
        String logStr = appender.getOutput();
        String expectedString = "getAllDatabases_()=";
        Assert.assertTrue(logStr + " should contain <" + expectedString, logStr.contains(expectedString));
        // reset the log buffer, verify new dump without any api call does not contain func
        appender.reset();
        hm.dumpAndClearMetaCallTiming("test");
        logStr = appender.getOutput();
        Assert.assertFalse(logStr + " should not contain <" + expectedString, logStr.contains(expectedString));
    } finally {
        loggerConfig.setLevel(oldLevel);
        ctx.updateLoggers();
        appender.removeFromLogger(logger.getName());
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Level(org.apache.logging.log4j.Level) Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 4 with LoggerContext

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

the class TestLog4j2Appenders method setup.

@Before
public void setup() {
    // programmatically set root logger level to INFO. By default if log4j2-test.properties is not
    // available root logger will use ERROR log level
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    loggerConfig.setLevel(Level.INFO);
    ctx.updateLoggers();
}
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) Before(org.junit.Before)

Example 5 with LoggerContext

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

the class LogConfigManager method resetLogLevels.

// function called on timer to reset log levels last set to DEBUG
// also called from processLogConfigChange
public void resetLogLevels() {
    TreeMap<String, LogLevel> latestLogLevelMap = latestLogConfig.get();
    LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    for (String loggerName : latestLogLevelMap.descendingKeySet()) {
        LogLevel loggerSetting = latestLogLevelMap.get(loggerName);
        long timeout = loggerSetting.get_reset_log_level_timeout_epoch();
        String resetLogLevel = loggerSetting.get_reset_log_level();
        if (timeout < Time.currentTimeMillis()) {
            LOG.info("{}: Resetting level to {}", loggerName, resetLogLevel);
            setLoggerLevel(loggerContext, loggerName, resetLogLevel);
        }
        latestLogConfig.getAndUpdate(input -> {
            TreeMap<String, LogLevel> result = new TreeMap<>(input);
            result.remove(loggerName);
            return result;
        });
    }
    loggerContext.updateLoggers();
}
Also used : TreeMap(java.util.TreeMap) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LogLevel(org.apache.storm.generated.LogLevel)

Aggregations

LoggerContext (org.apache.logging.log4j.core.LoggerContext)156 Configuration (org.apache.logging.log4j.core.config.Configuration)53 Test (org.junit.Test)33 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)32 Appender (org.apache.logging.log4j.core.Appender)15 File (java.io.File)12 IOException (java.io.IOException)11 Logger (org.apache.logging.log4j.Logger)11 BeforeClass (org.junit.BeforeClass)11 Map (java.util.Map)9 Level (org.apache.logging.log4j.Level)8 LogEvent (org.apache.logging.log4j.core.LogEvent)8 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)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 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)5 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)5 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4