Search in sources :

Example 6 with ConfigurationSource

use of org.apache.logging.log4j.core.config.ConfigurationSource in project metrics by dropwizard.

the class InstrumentedAppenderConfigTest method setUp.

@Before
public void setUp() throws Exception {
    source = new ConfigurationSource(this.getClass().getClassLoader().getResourceAsStream("log4j2-testconfig.xml"));
    context = Configurator.initialize(null, source);
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) Before(org.junit.Before)

Example 7 with ConfigurationSource

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

the class BuiltConfiguration method setMonitorInterval.

public void setMonitorInterval(final int intervalSeconds) {
    if (this instanceof Reconfigurable && intervalSeconds > 0) {
        final ConfigurationSource configSource = getConfigurationSource();
        if (configSource != null) {
            final File configFile = configSource.getFile();
            if (intervalSeconds > 0) {
                getWatchManager().setIntervalSeconds(intervalSeconds);
                if (configFile != null) {
                    final FileWatcher watcher = new ConfiguratonFileWatcher((Reconfigurable) this, listeners);
                    getWatchManager().watchFile(configFile, watcher);
                }
            }
        }
    }
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) FileWatcher(org.apache.logging.log4j.core.util.FileWatcher) ConfiguratonFileWatcher(org.apache.logging.log4j.core.config.ConfiguratonFileWatcher) File(java.io.File) ConfiguratonFileWatcher(org.apache.logging.log4j.core.config.ConfiguratonFileWatcher) Reconfigurable(org.apache.logging.log4j.core.config.Reconfigurable)

Example 8 with ConfigurationSource

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

the class LoggerContextAdmin method getConfigText.

@Override
public String getConfigText(final String charsetName) throws IOException {
    try {
        final ConfigurationSource source = loggerContext.getConfiguration().getConfigurationSource();
        final ConfigurationSource copy = source.resetInputStream();
        final Charset charset = Charset.forName(charsetName);
        return readContents(copy.getInputStream(), charset);
    } catch (final Exception ex) {
        final StringWriter sw = new StringWriter(BUFFER_SIZE);
        ex.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) StringWriter(java.io.StringWriter) Charset(java.nio.charset.Charset) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 9 with ConfigurationSource

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

the class LoggerContextAdmin method setConfigText.

@Override
public void setConfigText(final String configText, final String charsetName) {
    LOGGER.debug("---------");
    LOGGER.debug("Remote request to reconfigure from config text.");
    try {
        final InputStream in = new ByteArrayInputStream(configText.getBytes(charsetName));
        final ConfigurationSource source = new ConfigurationSource(in);
        final Configuration updated = ConfigurationFactory.getInstance().getConfiguration(loggerContext, source);
        loggerContext.start(updated);
        LOGGER.debug("Completed remote request to reconfigure from config text.");
    } catch (final Exception ex) {
        final String msg = "Could not reconfigure from config text";
        LOGGER.error(msg, ex);
        throw new IllegalArgumentException(msg, ex);
    }
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) Configuration(org.apache.logging.log4j.core.config.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 10 with ConfigurationSource

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

the class LoggerContextAdmin method setConfigLocationUri.

@Override
public void setConfigLocationUri(final String configLocation) throws URISyntaxException, IOException {
    if (configLocation == null || configLocation.isEmpty()) {
        throw new IllegalArgumentException("Missing configuration location");
    }
    LOGGER.debug("---------");
    LOGGER.debug("Remote request to reconfigure using location " + configLocation);
    final File configFile = new File(configLocation);
    ConfigurationSource configSource = null;
    if (configFile.exists()) {
        LOGGER.debug("Opening config file {}", configFile.getAbsolutePath());
        configSource = new ConfigurationSource(new FileInputStream(configFile), configFile);
    } else {
        final URL configURL = new URL(configLocation);
        LOGGER.debug("Opening config URL {}", configURL);
        configSource = new ConfigurationSource(configURL.openStream(), configURL);
    }
    final Configuration config = ConfigurationFactory.getInstance().getConfiguration(loggerContext, configSource);
    loggerContext.start(config);
    LOGGER.debug("Completed remote request to reconfigure.");
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) Configuration(org.apache.logging.log4j.core.config.Configuration) File(java.io.File) FileInputStream(java.io.FileInputStream) 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