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