Search in sources :

Example 1 with ConfigurationSource

use of org.apache.logging.log4j.core.config.ConfigurationSource 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)

Example 2 with ConfigurationSource

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

the class CompositeConfiguration method reconfigure.

@Override
public Configuration reconfigure() {
    LOGGER.debug("Reconfiguring composite configuration");
    final List<AbstractConfiguration> configs = new ArrayList<>();
    final ConfigurationFactory factory = ConfigurationFactory.getInstance();
    for (final AbstractConfiguration config : configurations) {
        final ConfigurationSource source = config.getConfigurationSource();
        final URI sourceURI = source.getURI();
        Configuration currentConfig;
        if (sourceURI != null) {
            LOGGER.warn("Unable to determine URI for configuration {}, changes to it will be ignored", config.getName());
            currentConfig = factory.getConfiguration(getLoggerContext(), config.getName(), sourceURI);
            if (currentConfig == null) {
                LOGGER.warn("Unable to reload configuration {}, changes to it will be ignored", config.getName());
                currentConfig = config;
            }
        } else {
            currentConfig = config;
        }
        configs.add((AbstractConfiguration) currentConfig);
    }
    return new CompositeConfiguration(configs);
}
Also used : AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) StatusConfiguration(org.apache.logging.log4j.core.config.status.StatusConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) ArrayList(java.util.ArrayList) ConfigurationFactory(org.apache.logging.log4j.core.config.ConfigurationFactory) URI(java.net.URI)

Example 3 with ConfigurationSource

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

the class XmlConfiguration method reconfigure.

@Override
public Configuration reconfigure() {
    try {
        final ConfigurationSource source = getConfigurationSource().resetInputStream();
        if (source == null) {
            return null;
        }
        final XmlConfiguration config = new XmlConfiguration(getLoggerContext(), source);
        return config.rootElement == null ? null : config;
    } catch (final IOException ex) {
        LOGGER.error("Cannot locate file {}", getConfigurationSource(), ex);
    }
    return null;
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) IOException(java.io.IOException)

Example 4 with ConfigurationSource

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

the class PropertiesConfiguration method reconfigure.

@Override
public Configuration reconfigure() {
    try {
        final ConfigurationSource source = getConfigurationSource().resetInputStream();
        if (source == null) {
            return null;
        }
        final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
        final PropertiesConfiguration config = factory.getConfiguration(getLoggerContext(), source);
        return config == null || config.getState() != State.INITIALIZING ? null : config;
    } catch (final IOException ex) {
        LOGGER.error("Cannot locate file {}: {}", getConfigurationSource(), ex);
    }
    return null;
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) IOException(java.io.IOException)

Example 5 with ConfigurationSource

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

the class Log4jLookup method lookup.

@Override
public String lookup(final LogEvent event, final String key) {
    if (configuration != null) {
        final ConfigurationSource configSrc = configuration.getConfigurationSource();
        final File file = configSrc.getFile();
        if (file != null) {
            switch(key) {
                case KEY_CONFIG_LOCATION:
                    return file.getAbsolutePath();
                case KEY_CONFIG_PARENT_LOCATION:
                    return file.getParentFile().getAbsolutePath();
                default:
                    return null;
            }
        }
        final URL url = configSrc.getURL();
        if (url != null) {
            try {
                switch(key) {
                    case KEY_CONFIG_LOCATION:
                        return asPath(url.toURI());
                    case KEY_CONFIG_PARENT_LOCATION:
                        return asPath(getParent(url.toURI()));
                    default:
                        return null;
                }
            } catch (final URISyntaxException use) {
                LOGGER.error(use);
                return null;
            }
        }
    }
    return null;
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Aggregations

ConfigurationSource (org.apache.logging.log4j.core.config.ConfigurationSource)10 IOException (java.io.IOException)5 File (java.io.File)3 URISyntaxException (java.net.URISyntaxException)3 URL (java.net.URL)3 Configuration (org.apache.logging.log4j.core.config.Configuration)3 FileInputStream (java.io.FileInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)1 ConfigurationFactory (org.apache.logging.log4j.core.config.ConfigurationFactory)1 ConfiguratonFileWatcher (org.apache.logging.log4j.core.config.ConfiguratonFileWatcher)1 Reconfigurable (org.apache.logging.log4j.core.config.Reconfigurable)1 StatusConfiguration (org.apache.logging.log4j.core.config.status.StatusConfiguration)1