Search in sources :

Example 1 with CompositeConfiguration

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

the class CompositeConfigurationTest method compositeLogger.

/*
    @Test
    public void compositeConfigurationUsed() {
        final LoggerContextRule lcr = new LoggerContextRule(
                "classpath:log4j-comp-appender.xml,log4j-comp-appender.json");
        Statement test = new Statement() {
            @Override
            public void evaluate() throws Throwable {
                assertTrue(lcr.getConfiguration() instanceof CompositeConfiguration);
            }
        };
        runTest(lcr, test);
    }

    @Test
    public void compositeProperties() {
        final LoggerContextRule lcr = new LoggerContextRule(
                "classpath:log4j-comp-properties.xml,log4j-comp-properties.json");
        Statement test = new Statement() {
            @Override
            public void evaluate() throws Throwable {
                CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
                assertEquals("json", config.getStrSubstitutor().replace("${propertyShared}"));
                assertEquals("xml", config.getStrSubstitutor().replace("${propertyXml}"));
                assertEquals("json", config.getStrSubstitutor().replace("${propertyJson}"));
            }
        };
        runTest(lcr, test);
    }

    @Test
    public void compositeAppenders() {
        final LoggerContextRule lcr = new LoggerContextRule(
                "classpath:log4j-comp-appender.xml,log4j-comp-appender.json");
        Statement test = new Statement() {
            @Override
            public void evaluate() throws Throwable {
                CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
                Map<String, Appender> appender = config.getAppenders();
                assertEquals(3, appender.size());
                assertTrue(appender.get("STDOUT") instanceof ConsoleAppender);
                assertTrue(appender.get("File") instanceof FileAppender);
                assertTrue(appender.get("Override") instanceof RollingFileAppender);
            }
        };
        runTest(lcr, test);
    }
*/
@Test
public void compositeLogger() {
    final LoggerContextRule lcr = new LoggerContextRule("classpath:log4j-comp-logger.xml,log4j-comp-logger.json");
    final Statement test = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
            Map<String, Appender> appendersMap = config.getLogger("cat1").getAppenders();
            assertEquals("Expected 2 Appender references for cat1 but got " + appendersMap.size(), 2, appendersMap.size());
            assertTrue(appendersMap.get("STDOUT") instanceof ConsoleAppender);
            Filter loggerFilter = config.getLogger("cat1").getFilter();
            assertTrue(loggerFilter instanceof RegexFilter);
            assertEquals(loggerFilter.getOnMatch(), Filter.Result.DENY);
            appendersMap = config.getLogger("cat2").getAppenders();
            assertEquals("Expected 1 Appender reference for cat2 but got " + appendersMap.size(), 1, appendersMap.size());
            assertTrue(appendersMap.get("File") instanceof FileAppender);
            appendersMap = config.getLogger("cat3").getAppenders();
            assertEquals("Expected 1 Appender reference for cat3 but got " + appendersMap.size(), 1, appendersMap.size());
            assertTrue(appendersMap.get("File") instanceof FileAppender);
            appendersMap = config.getRootLogger().getAppenders();
            assertEquals("Expected 2 Appender references for the root logger but got " + appendersMap.size(), 2, appendersMap.size());
            assertTrue(appendersMap.get("File") instanceof FileAppender);
            assertTrue(appendersMap.get("STDOUT") instanceof ConsoleAppender);
            assertEquals("Expected COMPOSITE_SOURCE for composite configuration but got " + config.getConfigurationSource(), config.getConfigurationSource(), ConfigurationSource.COMPOSITE_SOURCE);
        }
    };
    runTest(lcr, test);
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Filter(org.apache.logging.log4j.core.Filter) RegexFilter(org.apache.logging.log4j.core.filter.RegexFilter) Statement(org.junit.runners.model.Statement) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) RegexFilter(org.apache.logging.log4j.core.filter.RegexFilter) LoggerContextRule(org.apache.logging.log4j.core.test.junit.LoggerContextRule) Test(org.junit.Test)

Example 2 with CompositeConfiguration

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

the class CompositeConfigurationTest method testAttributeCheckWhenMergingConfigurations.

@Test
public void testAttributeCheckWhenMergingConfigurations() {
    final LoggerContextRule lcr = new LoggerContextRule("classpath:log4j-comp-root-loggers.xml,log4j-comp-logger.json");
    final Statement test = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            try {
                final CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
                Assert.assertNotNull(config);
            } catch (final NullPointerException e) {
                fail("Should not throw NullPointerException when there are different nodes.");
            }
        }
    };
    runTest(lcr, test);
}
Also used : Statement(org.junit.runners.model.Statement) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) LoggerContextRule(org.apache.logging.log4j.core.test.junit.LoggerContextRule) Test(org.junit.Test)

Example 3 with CompositeConfiguration

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

the class CompositeConfigurationTest method testAppenderRefFilterMerge.

@Test
public void testAppenderRefFilterMerge() {
    final LoggerContextRule lcr = new LoggerContextRule("classpath:log4j-comp-logger-ref.xml,log4j-comp-logger-ref.json");
    final Statement test = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
            final List<AppenderRef> appenderRefList = config.getLogger("cat1").getAppenderRefs();
            final AppenderRef appenderRef = getAppenderRef(appenderRefList, "STDOUT");
            assertTrue("Expected cat1 STDOUT appenderRef to have a regex filter", appenderRef.getFilter() != null && appenderRef.getFilter() instanceof RegexFilter);
        }
    };
    runTest(lcr, test);
}
Also used : Statement(org.junit.runners.model.Statement) CompositeConfiguration(org.apache.logging.log4j.core.config.composite.CompositeConfiguration) RegexFilter(org.apache.logging.log4j.core.filter.RegexFilter) LoggerContextRule(org.apache.logging.log4j.core.test.junit.LoggerContextRule) Test(org.junit.Test)

Example 4 with CompositeConfiguration

use of org.apache.logging.log4j.core.config.composite.CompositeConfiguration 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 != null) {
                    if (currentReadConfiguration instanceof DefaultConfiguration) {
                        LOGGER.warn("Unable to locate configuration {}, ignoring", configLocation.toString());
                    } else 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);
                    }
                } else {
                    LOGGER.info("Unable to access configuration {}, ignoring", configLocation.toString());
                }
            }
            if (configurations.size() == 0) {
                LOGGER.error("No configurations could be created for {}", configLocations.toString());
            } else if (configurations.size() == 1) {
                final AbstractConfiguration config = configurations.get(0);
                LOGGER.debug("Starting LoggerContext[name={}] from configuration at {}", ctx.getName(), config.getConfigurationSource().getLocation());
                ctx.start(config);
            } else {
                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) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) 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) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) URI(java.net.URI)

Example 5 with CompositeConfiguration

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

CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)9 ArrayList (java.util.ArrayList)5 LoggerContext (org.apache.logging.log4j.core.LoggerContext)5 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)5 IOException (java.io.IOException)4 LoggerContextRule (org.apache.logging.log4j.core.test.junit.LoggerContextRule)4 Test (org.junit.Test)4 Statement (org.junit.runners.model.Statement)4 Configuration (org.apache.logging.log4j.core.config.Configuration)3 FileVisitOption (java.nio.file.FileVisitOption)2 FileVisitResult (java.nio.file.FileVisitResult)2 Path (java.nio.file.Path)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 ConfigurationSource (org.apache.logging.log4j.core.config.ConfigurationSource)2 PropertiesConfiguration (org.apache.logging.log4j.core.config.properties.PropertiesConfiguration)2 PropertiesConfigurationFactory (org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory)2 RegexFilter (org.apache.logging.log4j.core.filter.RegexFilter)2 UserException (org.elasticsearch.cli.UserException)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1