BAEL-41: Added including tests:

- rolling file appender using XML layout
- async file appender using JSON layout
This commit is contained in:
Christian Rädel 2016-10-02 20:50:41 +02:00
parent 746e28de40
commit a929225e13
5 changed files with 61 additions and 17 deletions

View File

@ -31,6 +31,13 @@
<version>2.8.2</version>
</dependency>
<!-- This is used by XMLLayout. -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.2</version>
</dependency>
<!-- This is used for testing only. -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>

View File

@ -1,19 +1,14 @@
package com.baeldung.logging.log4j2.tests;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.junit.LoggerContextRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.assertTrue;
@ -21,7 +16,7 @@ import static org.junit.Assert.assertTrue;
public class AsyncFileAppenderUsingJsonLayoutTest {
@Rule
public LoggerContextRule contextRule = new LoggerContextRule("log4j2-async-file-appender_json-layout_colored.xml");
public LoggerContextRule contextRule = new LoggerContextRule("log4j2-async-file-appender_json-layout.xml");
@Test
public void givenLoggerWithAsyncConfig_shouldLogToJsonFile() throws Exception {
@ -30,16 +25,7 @@ public class AsyncFileAppenderUsingJsonLayoutTest {
for (int i = 0; i < count; i++) {
logger.info("This is async JSON message #{} at INFO level.", count);
}
ObjectMapper objectMapper = new ObjectMapper();
List<LogEvent> logEvents = Files.readAllLines(Paths.get("target/logfile.json")).stream()
.map(s -> {
try {
return objectMapper.readValue(s.getBytes(), LogEvent.class);
} catch (IOException e) {
throw new RuntimeException("Failed to import LogEvent!", e);
}
})
.collect(Collectors.toList());
assertTrue(logEvents.size() <= count);
long logEventsCount = Files.lines(Paths.get("target/logfile.json")).count();
assertTrue(logEventsCount == count);
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.logging.log4j2.tests;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.junit.LoggerContextRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import static org.junit.Assert.assertTrue;
@RunWith(JUnit4.class)
public class RollingFileAppenderUsingXMLLayoutTest {
@Rule
public LoggerContextRule contextRule = new LoggerContextRule("log4j2-rolling-file-appender_xml-layout.xml");
@Test
public void givenLoggerWithRollingFileConfig_shouldLogToXMLFile() throws Exception {
Logger logger = contextRule.getLogger(getClass().getSimpleName());
final int count = 88;
for (int i = 0; i < count; i++) {
logger.info("This is rolling file XML message #{} at INFO level.", count);
}
String[] logEvents = Files.readAllLines(Paths.get("target/logfile.xml")).stream()
.collect(Collectors.joining(System.lineSeparator()))
.split("\\n\\n+");
assertTrue(logEvents.length == count);
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<RollingFile name="XMLLogfile" fileName="target/logfile.xml"
filePattern="target/logfile-%d{yyyy-MM-dd}-%i.log.gz">
<XMLLayout/>
<Policies>
<SizeBasedTriggeringPolicy size="256 kB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="XMLLogfile"/>
</Root>
</Loggers>
</Configuration>