use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class PropertiesConfigurationRootLoggerOnlyTest 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() == 1);
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 YamlLayoutTest method testEscapeLayout.
@Test
public void testEscapeLayout() throws Exception {
final Map<String, Appender> appenders = this.rootLogger.getAppenders();
for (final Appender appender : appenders.values()) {
this.rootLogger.removeAppender(appender);
}
final Configuration configuration = rootLogger.getContext().getConfiguration();
// set up appender
final AbstractJacksonLayout layout = YamlLayout.createLayout(configuration, true, true, null, null, null, true);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
this.rootLogger.addAppender(appender);
this.rootLogger.setLevel(Level.DEBUG);
// output starting message
this.rootLogger.debug("Here is a quote ' and then a double quote \"");
appender.stop();
final List<String> list = appender.getMessages();
this.checkAt("---", 0, list);
this.checkContains("level: \"DEBUG\"", list);
this.checkContains("message: \"Here is a quote ' and then a double quote \\\"\"", list);
this.checkContains("loggerFqcn: \"" + AbstractLogger.class.getName() + "\"", list);
for (final Appender app : appenders.values()) {
this.rootLogger.addAppender(app);
}
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class YamlLayoutTest method testLayout.
/**
* Test case for MDC conversion pattern.
*/
@Test
public void testLayout() throws Exception {
final Map<String, Appender> appenders = this.rootLogger.getAppenders();
for (final Appender appender : appenders.values()) {
this.rootLogger.removeAppender(appender);
}
final Configuration configuration = rootLogger.getContext().getConfiguration();
// set up appender
// Use [[ and ]] to test header and footer (instead of [ and ])
final AbstractJacksonLayout layout = YamlLayout.createLayout(configuration, true, true, "[[", "]]", null, true);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
this.rootLogger.addAppender(appender);
this.rootLogger.setLevel(Level.DEBUG);
// output starting message
this.rootLogger.debug("starting mdc pattern test");
this.rootLogger.debug("empty mdc");
ThreadContext.put("key1", "value1");
ThreadContext.put("key2", "value2");
this.rootLogger.debug("filled mdc");
ThreadContext.remove("key1");
ThreadContext.remove("key2");
this.rootLogger.error("finished mdc pattern test", new NullPointerException("test"));
appender.stop();
final List<String> list = appender.getMessages();
this.checkAt("---", 0, list);
this.checkContains("loggerFqcn: \"" + AbstractLogger.class.getName() + "\"", list);
this.checkContains("level: \"DEBUG\"", list);
this.checkContains("message: \"starting mdc pattern test\"", list);
for (final Appender app : appenders.values()) {
this.rootLogger.addAppender(app);
}
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class JsonLayoutTest method testLayout.
/**
* Test case for MDC conversion pattern.
*/
@Test
public void testLayout() throws Exception {
final Map<String, Appender> appenders = this.rootLogger.getAppenders();
for (final Appender appender : appenders.values()) {
this.rootLogger.removeAppender(appender);
}
final Configuration configuration = rootLogger.getContext().getConfiguration();
// set up appender
// Use [[ and ]] to test header and footer (instead of [ and ])
final boolean propertiesAsList = false;
// @formatter:off
final AbstractJacksonLayout layout = JsonLayout.newBuilder().setConfiguration(configuration).setLocationInfo(true).setProperties(true).setPropertiesAsList(propertiesAsList).setComplete(true).setCompact(false).setEventEol(false).setHeader("[[".getBytes(Charset.defaultCharset())).setFooter("]]".getBytes(Charset.defaultCharset())).setIncludeStacktrace(true).build();
// @formatter:on
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
this.rootLogger.addAppender(appender);
this.rootLogger.setLevel(Level.DEBUG);
// output starting message
this.rootLogger.debug("starting mdc pattern test");
this.rootLogger.debug("empty mdc");
ThreadContext.put("key1", "value1");
ThreadContext.put("key2", "value2");
this.rootLogger.debug("filled mdc");
ThreadContext.remove("key1");
ThreadContext.remove("key2");
this.rootLogger.error("finished mdc pattern test", new NullPointerException("test"));
appender.stop();
final List<String> list = appender.getMessages();
this.checkAt("[[", 0, list);
this.checkAt("{", 1, list);
this.checkContains("\"loggerFqcn\" : \"" + AbstractLogger.class.getName() + "\",", list);
this.checkContains("\"level\" : \"DEBUG\",", list);
this.checkContains("\"message\" : \"starting mdc pattern test\",", list);
for (final Appender app : appenders.values()) {
this.rootLogger.addAppender(app);
}
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class CsvLogEventLayoutTest method testLayout.
private void testLayout(final CSVFormat format, final AbstractCsvLayout layout, final String header, final String footer) {
final Map<String, Appender> appenders = root.getAppenders();
for (final Appender appender : appenders.values()) {
root.removeAppender(appender);
}
// set up appender
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
root.addAppender(appender);
root.setLevel(Level.DEBUG);
root.debug("one={}, two={}, three={}", 1, 2, 3);
root.info("Hello");
appender.stop();
final List<String> list = appender.getMessages();
final boolean hasHeaderSerializer = layout.getHeaderSerializer() != null;
final boolean hasFooterSerializer = layout.getFooterSerializer() != null;
final int headerOffset = hasHeaderSerializer ? 1 : 0;
final String event0 = list.get(0 + headerOffset);
final String event1 = list.get(1 + headerOffset);
final char del = format.getDelimiter();
Assert.assertTrue(event0, event0.contains(del + "DEBUG" + del));
final String quote = del == ',' ? "\"" : "";
Assert.assertTrue(event0, event0.contains(del + quote + "one=1, two=2, three=3" + quote + del));
Assert.assertTrue(event1, event1.contains(del + "INFO" + del));
if (hasHeaderSerializer && header == null) {
Assert.fail();
}
if (!hasHeaderSerializer && header != null) {
Assert.fail();
}
if (hasFooterSerializer && footer == null) {
Assert.fail();
}
if (!hasFooterSerializer && footer != null) {
Assert.fail();
}
if (hasHeaderSerializer) {
Assert.assertEquals(list.toString(), header, list.get(0));
}
if (hasFooterSerializer) {
Assert.assertEquals(list.toString(), footer, list.get(list.size() - 1));
}
}
Aggregations