Search in sources :

Example 46 with Environment

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")));
}
Also used : Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) UserException(org.opensearch.cli.UserException) Settings(org.opensearch.common.settings.Settings)

Example 47 with Environment

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));
}
Also used : Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) Matchers.hasToString(org.hamcrest.Matchers.hasToString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Logger(org.apache.logging.log4j.Logger) Settings(org.opensearch.common.settings.Settings)

Example 48 with Environment

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());
}
Also used : Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) Matchers.hasToString(org.hamcrest.Matchers.hasToString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Logger(org.apache.logging.log4j.Logger) Settings(org.opensearch.common.settings.Settings)

Example 49 with Environment

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));
}
Also used : Path(java.nio.file.Path) Configuration(org.apache.logging.log4j.core.config.Configuration) Environment(org.opensearch.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.opensearch.common.settings.Settings) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 50 with Environment

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));
}
Also used : Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) Level(org.apache.logging.log4j.Level) Settings(org.opensearch.common.settings.Settings)

Aggregations

Environment (org.opensearch.env.Environment)142 TestEnvironment (org.opensearch.env.TestEnvironment)98 Path (java.nio.file.Path)80 Settings (org.opensearch.common.settings.Settings)79 Matchers.containsString (org.hamcrest.Matchers.containsString)69 Matchers.hasToString (org.hamcrest.Matchers.hasToString)40 NodeEnvironment (org.opensearch.env.NodeEnvironment)32 IOException (java.io.IOException)27 UserException (org.opensearch.cli.UserException)23 ClusterState (org.opensearch.cluster.ClusterState)22 IndexSettings (org.opensearch.index.IndexSettings)22 MockTerminal (org.opensearch.cli.MockTerminal)19 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)19 OpenSearchException (org.opensearch.OpenSearchException)18 Map (java.util.Map)16 OptionSet (joptsimple.OptionSet)15 DiscoverySettings (org.opensearch.node.Node.DiscoverySettings)14 Version (org.opensearch.Version)13 Files (java.nio.file.Files)11 Arrays (java.util.Arrays)11