Search in sources :

Example 1 with Configuration

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

the class EvilLoggerConfigurationTests method testLoggingLevelsFromSettings.

public void testLoggingLevelsFromSettings() throws IOException, UserException {
    final Level rootLevel = randomFrom(Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR);
    final Level fooLevel = randomFrom(Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR);
    final Level barLevel = randomFrom(Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR);
    final Path configDir = getDataPath("minimal");
    final Settings settings = Settings.builder().put(Environment.PATH_CONF_SETTING.getKey(), configDir.toAbsolutePath()).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put("logger.level", rootLevel.name()).put("logger.foo", fooLevel.name()).put("logger.bar", barLevel.name()).build();
    final Environment environment = new Environment(settings);
    LogConfigurator.configure(environment);
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    final Map<String, LoggerConfig> loggerConfigs = config.getLoggers();
    assertThat(loggerConfigs.size(), equalTo(3));
    assertThat(loggerConfigs, hasKey(""));
    assertThat(loggerConfigs.get("").getLevel(), equalTo(rootLevel));
    assertThat(loggerConfigs, hasKey("foo"));
    assertThat(loggerConfigs.get("foo").getLevel(), equalTo(fooLevel));
    assertThat(loggerConfigs, hasKey("bar"));
    assertThat(loggerConfigs.get("bar").getLevel(), equalTo(barLevel));
    assertThat(ctx.getLogger(randomAsciiOfLength(16)).getLevel(), equalTo(rootLevel));
}
Also used : Path(java.nio.file.Path) Configuration(org.apache.logging.log4j.core.config.Configuration) Environment(org.elasticsearch.env.Environment) Level(org.apache.logging.log4j.Level) Matchers.hasToString(org.hamcrest.Matchers.hasToString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Settings(org.elasticsearch.common.settings.Settings) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 2 with Configuration

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

the class Loggers method addAppender.

public static void addAppender(final Logger logger, final Appender appender) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    config.addAppender(appender);
    LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
    if (!logger.getName().equals(loggerConfig.getName())) {
        loggerConfig = new LoggerConfig(logger.getName(), logger.getLevel(), true);
        config.addLogger(logger.getName(), loggerConfig);
    }
    loggerConfig.addAppender(appender, null, null);
    ctx.updateLoggers();
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Configuration.getConfiguration(javax.security.auth.login.Configuration.getConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 3 with Configuration

use of org.apache.logging.log4j.core.config.Configuration 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 4 with Configuration

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

the class YamlLayoutTest method testEscapeLayout.

@Test
public void testEscapeLayout() throws Exception {
    final Map<String, Appender> appenders = this.rootLogger.getAppenders();
    for (final Appender appender : appenders.values()) {
        this.rootLogger.removeAppender(appender);
    }
    final Configuration configuration = rootLogger.getContext().getConfiguration();
    // set up appender
    final AbstractJacksonLayout layout = YamlLayout.createLayout(configuration, true, true, null, null, null, true);
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    this.rootLogger.addAppender(appender);
    this.rootLogger.setLevel(Level.DEBUG);
    // output starting message
    this.rootLogger.debug("Here is a quote ' and then a double quote \"");
    appender.stop();
    final List<String> list = appender.getMessages();
    this.checkAt("---", 0, list);
    this.checkContains("level: \"DEBUG\"", list);
    this.checkContains("message: \"Here is a quote ' and then a double quote \\\"\"", list);
    this.checkContains("loggerFqcn: \"" + AbstractLogger.class.getName() + "\"", list);
    for (final Appender app : appenders.values()) {
        this.rootLogger.addAppender(app);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 5 with Configuration

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

the class YamlLayoutTest method testLayout.

/**
     * Test case for MDC conversion pattern.
     */
@Test
public void testLayout() throws Exception {
    final Map<String, Appender> appenders = this.rootLogger.getAppenders();
    for (final Appender appender : appenders.values()) {
        this.rootLogger.removeAppender(appender);
    }
    final Configuration configuration = rootLogger.getContext().getConfiguration();
    // set up appender
    // Use [[ and ]] to test header and footer (instead of [ and ])
    final AbstractJacksonLayout layout = YamlLayout.createLayout(configuration, true, true, "[[", "]]", null, true);
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    // set appender on root and set level to debug
    this.rootLogger.addAppender(appender);
    this.rootLogger.setLevel(Level.DEBUG);
    // output starting message
    this.rootLogger.debug("starting mdc pattern test");
    this.rootLogger.debug("empty mdc");
    ThreadContext.put("key1", "value1");
    ThreadContext.put("key2", "value2");
    this.rootLogger.debug("filled mdc");
    ThreadContext.remove("key1");
    ThreadContext.remove("key2");
    this.rootLogger.error("finished mdc pattern test", new NullPointerException("test"));
    appender.stop();
    final List<String> list = appender.getMessages();
    this.checkAt("---", 0, list);
    this.checkContains("loggerFqcn: \"" + AbstractLogger.class.getName() + "\"", list);
    this.checkContains("level: \"DEBUG\"", list);
    this.checkContains("message: \"starting mdc pattern test\"", list);
    for (final Appender app : appenders.values()) {
        this.rootLogger.addAppender(app);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

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