use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.
the class MapFilterTest method testConfig.
@Test
@LoggerContextSource("log4j2-mapfilter.xml")
public void testConfig(final Configuration config, @Named("LIST") final ListAppender app) {
final Filter filter = config.getFilter();
assertNotNull(filter, "No MapFilter");
assertTrue(filter instanceof MapFilter, "Not a MapFilter");
final MapFilter mapFilter = (MapFilter) filter;
assertFalse(mapFilter.isAnd(), "Should not be And filter");
final IndexedReadOnlyStringMap map = mapFilter.getStringMap();
assertNotNull(map, "No Map");
assertFalse(map.isEmpty(), "No elements in Map");
assertEquals(1, map.size(), "Incorrect number of elements in Map");
assertTrue(map.containsKey("eventId"), "Map does not contain key eventId");
assertEquals(2, map.<Collection<?>>getValue("eventId").size(), "List does not contain 2 elements");
final Logger logger = LogManager.getLogger(MapFilterTest.class);
final Map<String, String> eventMap = new HashMap<>();
eventMap.put("eventId", "Login");
logger.debug(new StringMapMessage(eventMap));
final List<String> msgs = app.getMessages();
assertNotNull(msgs, "No messages");
assertFalse(msgs.isEmpty(), "No messages");
}
use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.
the class PropertiesConfigurationTest method testRootLoggerOnly.
@Test
@LoggerContextSource("log4j2-properties-root-only.properties")
void testRootLoggerOnly(final Configuration config) {
assertEquals(config.getState(), LifeCycle.State.STARTED, "Incorrect State: " + config.getState());
final Map<String, Appender> appenders = config.getAppenders();
assertNotNull(appenders);
assertEquals(appenders.size(), 1, "Incorrect number of Appenders: " + appenders.size());
final Map<String, LoggerConfig> loggers = config.getLoggers();
assertNotNull(loggers);
assertEquals(loggers.size(), 1, "Incorrect number of LoggerConfigs: " + loggers.size());
final Filter filter = config.getFilter();
assertNotNull(filter, "No Filter");
assertThat(filter, instanceOf(ThresholdFilter.class));
final Logger logger = LogManager.getLogger(getClass());
logger.info("Welcome to Log4j!");
}
use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.
the class MemoryMappedFileAppenderTest method testMemMapLocation.
@Test
@LoggerContextSource("MemoryMappedFileAppenderLocationTest.xml")
void testMemMapLocation(final LoggerContext context) throws Exception {
final Logger log = context.getLogger(getClass());
final Path logFile = Paths.get("target", "MemoryMappedFileAppenderLocationTest.log");
final int expectedFileLength = Integers.ceilingNextPowerOfTwo(32000);
assertEquals(32768, expectedFileLength);
try {
log.warn("Test log1");
assertTrue(Files.exists(logFile));
assertEquals(expectedFileLength, Files.size(logFile));
log.warn("Test log2");
assertEquals(expectedFileLength, Files.size(logFile));
} finally {
context.stop();
}
int expectedLength = Strings.isEmpty(System.getProperty("jdk.module.path")) ? 272 : 332;
assertEquals(expectedLength + 2 * System.lineSeparator().length(), Files.size(logFile), "Expected file size to shrink");
final List<String> lines = Files.readAllLines(logFile);
assertEquals(2, lines.size());
assertTrue(lines.get(0).endsWith("org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderTest.testMemMapLocation(MemoryMappedFileAppenderTest.java:105): Test log1"));
assertTrue(lines.get(1).endsWith("org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderTest.testMemMapLocation(MemoryMappedFileAppenderTest.java:108): Test log2"));
}
use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.
the class MemoryMappedFileAppenderTest method testMemMapBasics.
@Test
@LoggerContextSource("MemoryMappedFileAppenderTest.xml")
public void testMemMapBasics(final LoggerContext context) throws Exception {
final Logger log = context.getLogger(getClass());
final Path logFile = Paths.get("target", "MemoryMappedFileAppenderTest.log");
try {
log.warn("Test log1");
assertTrue(Files.exists(logFile));
assertEquals(MemoryMappedFileManager.DEFAULT_REGION_LENGTH, Files.size(logFile));
log.warn("Test log2");
assertEquals(MemoryMappedFileManager.DEFAULT_REGION_LENGTH, Files.size(logFile));
} finally {
context.stop();
}
final int LINESEP = System.lineSeparator().length();
assertEquals(18 + 2 * LINESEP, Files.size(logFile));
final List<String> lines = Files.readAllLines(logFile);
assertThat(lines, both(hasSize(2)).and(contains("Test log1", "Test log2")));
}
use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.
the class AsyncAppenderTest method testNoLocationInformation.
@Test
@LoggerContextSource("log4j-asynch-no-location.xml")
public void testNoLocationInformation(final LoggerContext context, @Named("List") final ListAppender appender) throws InterruptedException {
final ExtendedLogger logger = context.getLogger(getClass());
logger.error("This is a test");
logger.warn("Hello world!");
final List<String> messages;
try {
messages = appender.getMessages(2, 2, TimeUnit.SECONDS);
} finally {
appender.clear();
}
assertNotNull(messages);
assertEquals(2, messages.size());
assertEquals("? This is a test", messages.get(0));
assertEquals("? Hello world!", messages.get(1));
}
Aggregations