Search in sources :

Example 86 with Appender

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

the class Rfc5424LayoutTest method testDiscardEmptyLoggerFields.

@Test
public void testDiscardEmptyLoggerFields() {
    final String mdcId = "RequestContext";
    Arrays.asList("[BAZ@32473 baz=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]" + "[RequestContext@3692 bar=\"org.apache.logging.log4j.core.layout.Rfc5424LayoutTest.testLoggerFields\"]");
    for (final Appender appender : root.getAppenders().values()) {
        root.removeAppender(appender);
    }
    final LoggerFields[] loggerFields = new LoggerFields[] { LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("dummy", Strings.EMPTY), new KeyValuePair("empty", Strings.EMPTY) }, "SD-ID", "32473", true), LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("baz", "%C.%M"), new KeyValuePair("baz", "%C.%M") }, "BAZ", "32473", false), LoggerFields.createLoggerFields(new KeyValuePair[] { new KeyValuePair("bar", "%C.%M") }, null, null, false) };
    final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, mdcId, null, null, true, null, "ATM", null, "key1, key2, locale", null, null, null, false, loggerFields, null);
    final ListAppender appender = new ListAppender("List", null, layout, true, false);
    appender.start();
    root.addAppender(appender);
    root.setLevel(Level.DEBUG);
    root.info("starting logger fields test");
    try {
        final List<String> list = appender.getMessages();
        assertTrue("Not enough list entries", list.size() > 0);
        final String message = list.get(0);
        Assert.assertTrue("SD-ID should have been discarded", !message.contains("SD-ID"));
        Assert.assertTrue("BAZ should have been included", message.contains("BAZ"));
        Assert.assertTrue(mdcId + "should have been included", message.contains(mdcId));
        appender.clear();
    } finally {
        root.removeAppender(appender);
        appender.stop();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 87 with Appender

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

the class MissingRootLoggerTest method testMissingRootLogger.

@Test
public void testMissingRootLogger() throws Exception {
    final LoggerContext ctx = context.getLoggerContext();
    final Logger logger = ctx.getLogger("sample.Logger1");
    assertTrue("Logger should have the INFO level enabled", logger.isInfoEnabled());
    assertFalse("Logger should have the DEBUG level disabled", logger.isDebugEnabled());
    final Configuration config = ctx.getConfiguration();
    assertNotNull("Config not null", config);
    //        final String MISSINGROOT = "MissingRootTest";
    //        assertTrue("Incorrect Configuration. Expected " + MISSINGROOT + " but found " + config.getName(),
    //                MISSINGROOT.equals(config.getName()));
    final Map<String, Appender> map = config.getAppenders();
    assertNotNull("Appenders not null", map);
    assertThat("There should only be two appenders", map, hasSize(2));
    assertThat(map, hasKey("List"));
    assertThat(map, hasKey("DefaultConsole-2"));
    final Map<String, LoggerConfig> loggerMap = config.getLoggers();
    assertNotNull("loggerMap not null", loggerMap);
    assertThat("There should only be one configured logger", loggerMap, hasSize(1));
    // only the sample logger, no root logger in loggerMap!
    assertThat("contains key=sample", loggerMap, hasKey("sample"));
    final LoggerConfig sample = loggerMap.get("sample");
    final Map<String, Appender> sampleAppenders = sample.getAppenders();
    assertThat("The sample logger should only have one appender", sampleAppenders, hasSize(1));
    // sample only has List appender, not Console!
    assertThat("The sample appender should be a ListAppender", sampleAppenders, hasKey("List"));
    assertThat(config, is(instanceOf(AbstractConfiguration.class)));
    final AbstractConfiguration baseConfig = (AbstractConfiguration) config;
    final LoggerConfig root = baseConfig.getRootLogger();
    final Map<String, Appender> rootAppenders = root.getAppenders();
    assertThat("The root logger should only have one appender", rootAppenders, hasSize(1));
    // root only has Console appender!
    assertThat("The root appender should be a ConsoleAppender", rootAppenders, hasKey("DefaultConsole-2"));
    assertEquals(Level.ERROR, root.getLevel());
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 88 with Appender

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

the class TestConfigurator method testInitialize_InputStream_File.

@Test
public void testInitialize_InputStream_File() throws Exception {
    final File file = new File("target/test-classes/log4j2-config.xml");
    final InputStream is = new FileInputStream(file);
    final ConfigurationSource source = new ConfigurationSource(is, file);
    ctx = Configurator.initialize(null, source);
    LogManager.getLogger("org.apache.test.TestConfigurator");
    Configuration config = ctx.getConfiguration();
    assertNotNull("No configuration", config);
    assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
    final Map<String, Appender> map = config.getAppenders();
    assertNotNull("Appenders map should not be null.", map);
    assertThat(map, hasSize(greaterThan(0)));
    assertThat("Wrong configuration", map, hasKey("List"));
    Configurator.shutdown(ctx);
    config = ctx.getConfiguration();
    assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 89 with Appender

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

the class PropertiesConfigurationTest method testPropertiesConfiguration.

@Test
public void testPropertiesConfiguration() {
    final Configuration config = context.getConfiguration();
    assertNotNull("No configuration created", config);
    assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED);
    final Map<String, Appender> appenders = config.getAppenders();
    assertNotNull(appenders);
    assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1);
    final Map<String, LoggerConfig> loggers = config.getLoggers();
    assertNotNull(loggers);
    assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2);
    final Filter filter = config.getFilter();
    assertNotNull("No Filter", filter);
    assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter);
    final Logger logger = LogManager.getLogger(getClass());
    logger.info("Welcome to Log4j!");
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Configuration(org.apache.logging.log4j.core.config.Configuration) Filter(org.apache.logging.log4j.core.Filter) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Logger(org.apache.logging.log4j.Logger) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) Test(org.junit.Test)

Example 90 with Appender

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

the class RollingFilePropertiesTest method testPropertiesConfiguration.

@Test
public void testPropertiesConfiguration() {
    final Configuration config = context.getConfiguration();
    assertNotNull("No configuration created", config);
    assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED);
    final Map<String, Appender> appenders = config.getAppenders();
    assertNotNull(appenders);
    assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 3);
    final Map<String, LoggerConfig> loggers = config.getLoggers();
    assertNotNull(loggers);
    assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2);
    final Filter filter = config.getFilter();
    assertNotNull("No Filter", filter);
    assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter);
    final Logger logger = LogManager.getLogger(getClass());
    logger.info("Welcome to Log4j!");
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Configuration(org.apache.logging.log4j.core.config.Configuration) Filter(org.apache.logging.log4j.core.Filter) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Logger(org.apache.logging.log4j.Logger) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) Test(org.junit.Test)

Aggregations

Appender (org.apache.logging.log4j.core.Appender)98 Test (org.junit.Test)50 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)25 Configuration (org.apache.logging.log4j.core.config.Configuration)21 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)18 LoggerContext (org.apache.logging.log4j.core.LoggerContext)15 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)13 Logger (org.apache.logging.log4j.Logger)12 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)12 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)11 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Map (java.util.Map)8 Filter (org.apache.logging.log4j.core.Filter)8 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)8 URL (java.net.URL)7 IOException (java.io.IOException)5 LogEvent (org.apache.logging.log4j.core.LogEvent)5 Logger (org.apache.logging.log4j.core.Logger)5 AsyncAppender (org.apache.logging.log4j.core.appender.AsyncAppender)5