use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.
the class EvilLoggerConfigurationTests method testMissingConfigFile.
public void testMissingConfigFile() {
final Path configDir = getDataPath("does_not_exist");
final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
final Environment environment = new Environment(settings, configDir);
UserException e = expectThrows(UserException.class, () -> LogConfigurator.configure(environment));
assertThat(e, hasToString(containsString("no log4j2.properties found; tried")));
}
use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.
the class EvilLoggerConfigurationTests method testDefaults.
public void testDefaults() throws IOException, UserException {
final Path configDir = getDataPath("config");
final String level = randomFrom(Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR).toString();
final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put("logger.level", level).build();
final Environment environment = new Environment(settings, configDir);
LogConfigurator.configure(environment);
final String loggerName = "test";
final Logger logger = LogManager.getLogger(loggerName);
assertThat(logger.getLevel().toString(), equalTo(level));
}
use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.
the class EvilLoggerConfigurationTests method testResolveOrder.
// tests that custom settings are not overwritten by settings in the config file
public void testResolveOrder() throws Exception {
final Path configDir = getDataPath("config");
final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put("logger.test_resolve_order", "TRACE").build();
final Environment environment = new Environment(settings, configDir);
LogConfigurator.configure(environment);
// args should overwrite whatever is in the config
final String loggerName = "test_resolve_order";
final Logger logger = LogManager.getLogger(loggerName);
assertTrue(logger.isTraceEnabled());
}
use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.
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_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, configDir);
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(randomAlphaOfLength(16)).getLevel(), equalTo(rootLevel));
}
use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.
the class EvilLoggerConfigurationTests method testHierarchy.
public void testHierarchy() throws Exception {
final Path configDir = getDataPath("hierarchy");
final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
final Environment environment = new Environment(settings, configDir);
LogConfigurator.configure(environment);
assertThat(LogManager.getLogger("x").getLevel(), equalTo(Level.TRACE));
assertThat(LogManager.getLogger("x.y").getLevel(), equalTo(Level.DEBUG));
final Level level = randomFrom(Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR);
Loggers.setLevel(LogManager.getLogger("x"), level);
assertThat(LogManager.getLogger("x").getLevel(), equalTo(level));
assertThat(LogManager.getLogger("x.y").getLevel(), equalTo(level));
}
Aggregations