Search in sources :

Example 1 with AbstractConfiguration

use of org.apache.logging.log4j.core.config.AbstractConfiguration 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 2 with AbstractConfiguration

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

the class Log4jContextFactory method getContext.

public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext, final boolean currentContext, final List<URI> configLocations, final String name) {
    final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, null);
    if (externalContext != null && ctx.getExternalContext() == null) {
        ctx.setExternalContext(externalContext);
    }
    if (name != null) {
        ctx.setName(name);
    }
    if (ctx.getState() == LifeCycle.State.INITIALIZED) {
        if ((configLocations != null && !configLocations.isEmpty())) {
            ContextAnchor.THREAD_CONTEXT.set(ctx);
            final List<AbstractConfiguration> configurations = new ArrayList<>(configLocations.size());
            for (final URI configLocation : configLocations) {
                final Configuration currentReadConfiguration = ConfigurationFactory.getInstance().getConfiguration(ctx, name, configLocation);
                if (currentReadConfiguration instanceof AbstractConfiguration) {
                    configurations.add((AbstractConfiguration) currentReadConfiguration);
                } else {
                    LOGGER.error("Found configuration {}, which is not an AbstractConfiguration and can't be handled by CompositeConfiguration", configLocation);
                }
            }
            final CompositeConfiguration compositeConfiguration = new CompositeConfiguration(configurations);
            LOGGER.debug("Starting LoggerContext[name={}] from configurations at {}", ctx.getName(), configLocations);
            ctx.start(compositeConfiguration);
            ContextAnchor.THREAD_CONTEXT.remove();
        } else {
            ctx.start();
        }
    }
    return ctx;
}
Also used : AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) ArrayList(java.util.ArrayList) LoggerContext(org.apache.logging.log4j.core.LoggerContext) URI(java.net.URI)

Example 3 with AbstractConfiguration

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

the class LogConfigurator method configure.

private static void configure(final Settings settings, final Path configsPath, final Path logsPath) throws IOException, UserException {
    Objects.requireNonNull(settings);
    Objects.requireNonNull(configsPath);
    Objects.requireNonNull(logsPath);
    setLogConfigurationSystemProperty(logsPath, settings);
    // we initialize the status logger immediately otherwise Log4j will complain when we try to get the context
    configureStatusLogger();
    final LoggerContext context = (LoggerContext) LogManager.getContext(false);
    final List<AbstractConfiguration> configurations = new ArrayList<>();
    final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
    final Set<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    Files.walkFileTree(configsPath, options, Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            if (file.getFileName().toString().equals("log4j2.properties")) {
                configurations.add((PropertiesConfiguration) factory.getConfiguration(context, file.toString(), file.toUri()));
            }
            return FileVisitResult.CONTINUE;
        }
    });
    if (configurations.isEmpty()) {
        throw new UserException(ExitCodes.CONFIG, "no log4j2.properties found; tried [" + configsPath + "] and its subdirectories");
    }
    context.start(new CompositeConfiguration(configurations));
    configureLoggerLevels(settings);
}
Also used : Path(java.nio.file.Path) FileVisitOption(java.nio.file.FileVisitOption) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) LoggerContext(org.apache.logging.log4j.core.LoggerContext) PropertiesConfiguration(org.apache.logging.log4j.core.config.properties.PropertiesConfiguration) AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) PropertiesConfigurationFactory(org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory) UserException(org.elasticsearch.cli.UserException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 4 with AbstractConfiguration

use of org.apache.logging.log4j.core.config.AbstractConfiguration in project oxCore by GluuFederation.

the class LoggingHelper method configureConsoleAppender.

public static void configureConsoleAppender() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
    ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout());
    appender.start();
    config.addAppender(appender);
    AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) };
    LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, LogManager.ROOT_LOGGER_NAME, "true", refs, null, config, null);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig);
    ctx.updateLoggers();
}
Also used : AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 5 with AbstractConfiguration

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

the class CompositeConfiguration method setup.

@Override
public void setup() {
    final AbstractConfiguration targetConfiguration = configurations.get(0);
    staffChildConfiguration(targetConfiguration);
    final WatchManager watchManager = getWatchManager();
    final WatchManager targetWatchManager = targetConfiguration.getWatchManager();
    final FileWatcher fileWatcher = new ConfiguratonFileWatcher(this, listeners);
    if (targetWatchManager.getIntervalSeconds() > 0) {
        watchManager.setIntervalSeconds(targetWatchManager.getIntervalSeconds());
        final Map<File, FileWatcher> watchers = targetWatchManager.getWatchers();
        for (final Map.Entry<File, FileWatcher> entry : watchers.entrySet()) {
            if (entry.getValue() instanceof ConfiguratonFileWatcher) {
                watchManager.watchFile(entry.getKey(), fileWatcher);
            }
        }
    }
    for (final AbstractConfiguration sourceConfiguration : configurations.subList(1, configurations.size())) {
        staffChildConfiguration(sourceConfiguration);
        final Node sourceRoot = sourceConfiguration.getRootNode();
        mergeStrategy.mergConfigurations(rootNode, sourceRoot, getPluginManager());
        if (LOGGER.isEnabled(Level.ALL)) {
            final StringBuilder sb = new StringBuilder();
            printNodes("", rootNode, sb);
            System.out.println(sb.toString());
        }
        final int monitorInterval = sourceConfiguration.getWatchManager().getIntervalSeconds();
        if (monitorInterval > 0) {
            final int currentInterval = watchManager.getIntervalSeconds();
            if (currentInterval <= 0 || monitorInterval < currentInterval) {
                watchManager.setIntervalSeconds(monitorInterval);
            }
            final WatchManager sourceWatchManager = sourceConfiguration.getWatchManager();
            final Map<File, FileWatcher> watchers = sourceWatchManager.getWatchers();
            for (final Map.Entry<File, FileWatcher> entry : watchers.entrySet()) {
                if (entry.getValue() instanceof ConfiguratonFileWatcher) {
                    watchManager.watchFile(entry.getKey(), fileWatcher);
                }
            }
        }
    }
}
Also used : AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) FileWatcher(org.apache.logging.log4j.core.util.FileWatcher) ConfiguratonFileWatcher(org.apache.logging.log4j.core.config.ConfiguratonFileWatcher) Node(org.apache.logging.log4j.core.config.Node) ConfiguratonFileWatcher(org.apache.logging.log4j.core.config.ConfiguratonFileWatcher) File(java.io.File) Map(java.util.Map) WatchManager(org.apache.logging.log4j.core.util.WatchManager)

Aggregations

AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)5 ArrayList (java.util.ArrayList)3 LoggerContext (org.apache.logging.log4j.core.LoggerContext)3 URI (java.net.URI)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)2 File (java.io.File)1 IOException (java.io.IOException)1 FileVisitOption (java.nio.file.FileVisitOption)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 Map (java.util.Map)1 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)1 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)1 ConfigurationFactory (org.apache.logging.log4j.core.config.ConfigurationFactory)1 ConfigurationSource (org.apache.logging.log4j.core.config.ConfigurationSource)1 ConfiguratonFileWatcher (org.apache.logging.log4j.core.config.ConfiguratonFileWatcher)1 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)1 Node (org.apache.logging.log4j.core.config.Node)1