Search in sources :

Example 6 with LoggerContext

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

the class EvilLoggerTests method tearDown.

@Override
public void tearDown() throws Exception {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configurator.shutdown(context);
    super.tearDown();
}
Also used : LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 7 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext 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 8 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext 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 9 with LoggerContext

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

the class Bootstrap method setup.

private void setup(boolean addShutdownHook, Environment environment) throws BootstrapException {
    Settings settings = environment.settings();
    try {
        spawner.spawnNativePluginControllers(environment);
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                try {
                    spawner.close();
                } catch (IOException e) {
                    throw new ElasticsearchException("Failed to destroy spawned controllers", e);
                }
            }
        });
    } catch (IOException e) {
        throw new BootstrapException(e);
    }
    initializeNatives(environment.tmpFile(), BootstrapSettings.MEMORY_LOCK_SETTING.get(settings), BootstrapSettings.SYSTEM_CALL_FILTER_SETTING.get(settings), BootstrapSettings.CTRLHANDLER_SETTING.get(settings));
    // initialize probes before the security manager is installed
    initializeProbes();
    if (addShutdownHook) {
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                try {
                    IOUtils.close(node);
                    LoggerContext context = (LoggerContext) LogManager.getContext(false);
                    Configurator.shutdown(context);
                } catch (IOException ex) {
                    throw new ElasticsearchException("failed to stop node", ex);
                }
            }
        });
    }
    try {
        // look for jar hell
        JarHell.checkJarHell();
    } catch (IOException | URISyntaxException e) {
        throw new BootstrapException(e);
    }
    // Log ifconfig output before SecurityManager is installed
    IfConfig.logIfNecessary();
    // install SM after natives, shutdown hooks, etc.
    try {
        Security.configure(environment, BootstrapSettings.SECURITY_FILTER_BAD_DEFAULTS_SETTING.get(settings));
    } catch (IOException | NoSuchAlgorithmException e) {
        throw new BootstrapException(e);
    }
    node = new Node(environment) {

        @Override
        protected void validateNodeBeforeAcceptingRequests(final Settings settings, final BoundTransportAddress boundTransportAddress, List<BootstrapCheck> checks) throws NodeValidationException {
            BootstrapChecks.check(settings, boundTransportAddress, checks);
        }
    };
}
Also used : Node(org.elasticsearch.node.Node) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LoggerContext(org.apache.logging.log4j.core.LoggerContext) NodeValidationException(org.elasticsearch.node.NodeValidationException) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) Settings(org.elasticsearch.common.settings.Settings) SecureSettings(org.elasticsearch.common.settings.SecureSettings)

Example 10 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project spring-boot by spring-projects.

the class Log4J2LoggingSystem method loadConfiguration.

protected void loadConfiguration(String location, LogFile logFile) {
    Assert.notNull(location, "Location must not be null");
    try {
        LoggerContext ctx = getLoggerContext();
        URL url = ResourceUtils.getURL(location);
        ConfigurationSource source = getConfigurationSource(url);
        ctx.start(ConfigurationFactory.getInstance().getConfiguration(ctx, source));
    } catch (Exception ex) {
        throw new IllegalStateException("Could not initialize Log4J2 logging from " + location, ex);
    }
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) LoggerContext(org.apache.logging.log4j.core.LoggerContext) URL(java.net.URL) IOException(java.io.IOException)

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