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);
}
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;
}
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);
}
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();
}
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);
}
}
}
}
}
Aggregations