Search in sources :

Example 1 with PropertiesConfigurationFactory

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

Aggregations

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 ArrayList (java.util.ArrayList)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)1 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)1 PropertiesConfiguration (org.apache.logging.log4j.core.config.properties.PropertiesConfiguration)1 PropertiesConfigurationFactory (org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory)1 UserException (org.elasticsearch.cli.UserException)1