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));
}
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();
}
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();
}
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);
}
}
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);
}
}
Aggregations