Search in sources :

Example 1 with LoggerContextRule

use of org.apache.logging.log4j.core.test.junit.LoggerContextRule 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 LoggerContextRule

use of org.apache.logging.log4j.core.test.junit.LoggerContextRule 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 LoggerContextRule

use of org.apache.logging.log4j.core.test.junit.LoggerContextRule 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 LoggerContextRule

use of org.apache.logging.log4j.core.test.junit.LoggerContextRule in project logging-log4j2 by apache.

the class CompositeConfigurationTest method testMissingConfig.

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

        @Override
        public void evaluate() throws Throwable {
            final AbstractConfiguration config = (AbstractConfiguration) lcr.getConfiguration();
            assertNotNull("No configuration returned", config);
            // Test for Root log level override
            assertEquals("Expected Root logger log level to be ERROR", Level.ERROR, config.getRootLogger().getLevel());
            // Test for no cat2 level override
            final LoggerConfig cat2 = config.getLogger("cat2");
            assertEquals("Expected cat2 log level to be INFO", Level.DEBUG, cat2.getLevel());
        }
    };
    runTest(lcr, test);
}
Also used : Statement(org.junit.runners.model.Statement) LoggerContextRule(org.apache.logging.log4j.core.test.junit.LoggerContextRule) Test(org.junit.Test)

Example 5 with LoggerContextRule

use of org.apache.logging.log4j.core.test.junit.LoggerContextRule in project logging-log4j2 by apache.

the class CompositeConfigurationTest method testAttributeMergeForLoggers.

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

        @Override
        public void evaluate() throws Throwable {
            final CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration();
            // Test for Root log level override
            assertEquals("Expected Root logger log level to be WARN", Level.WARN, config.getRootLogger().getLevel());
            // Test for cat2 level override
            final LoggerConfig cat2 = config.getLogger("cat2");
            assertEquals("Expected cat2 log level to be INFO", Level.INFO, cat2.getLevel());
            // Test for cat2 additivity override
            assertTrue("Expected cat2 additivity to be true", cat2.isAdditive());
            // Regression
            // Check level on cat3 (not present in root config)
            assertEquals("Expected cat3 log level to be ERROR", Level.ERROR, config.getLogger("cat3").getLevel());
            // Check level on cat1 (not present in overridden config)
            assertEquals("Expected cat1 log level to be DEBUG", Level.DEBUG, config.getLogger("cat1").getLevel());
        }
    };
    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)

Aggregations

LoggerContextRule (org.apache.logging.log4j.core.test.junit.LoggerContextRule)5 Test (org.junit.Test)5 Statement (org.junit.runners.model.Statement)5 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)4 RegexFilter (org.apache.logging.log4j.core.filter.RegexFilter)2 Appender (org.apache.logging.log4j.core.Appender)1 Filter (org.apache.logging.log4j.core.Filter)1 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)1 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)1