BAEL-41: Added including tests:

- console appender using default layout
- console appender using colorized pattern layout
- async file appender using JSON layout
This commit is contained in:
Christian Rädel 2016-09-17 01:29:51 +02:00
parent 14f0c886c4
commit 6e97acc1ca
9 changed files with 232 additions and 0 deletions

58
logging/log4j2/pom.xml Normal file
View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>logging</artifactId>
<groupId>com.baeldung.logging</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>log4j2</artifactId>
<dependencies>
<!-- These two are the needed core components. -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<!-- This is used by JSONLayout. -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.2</version>
</dependency>
<!-- This is used for testing only. -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,45 @@
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;
@RunWith(JUnit4.class)
public class AsyncFileAppenderUsingJsonLayoutTest {
@Rule
public LoggerContextRule contextRule = new LoggerContextRule("log4j2-async-file-appender_json-layout_colored.xml");
@Test
public void givenLoggerWithAsyncConfig_shouldLogToJsonFile() throws Exception {
Logger logger = contextRule.getLogger(getClass().getSimpleName());
final int count = 88;
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);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.logging.log4j2.tests;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class ConsoleAppenderUsingDefaultLayoutTest {
@Test
public void givenLoggerWithDefaultConfig_shouldLogToConsole() throws Exception {
Logger logger = LogManager.getLogger(ConsoleAppenderUsingDefaultLayoutTest.class);
Exception e = new RuntimeException("This is only a test!");
logger.error("This is a simple message at ERROR level. This is the minimum visible level.", e);
}
}

View File

@ -0,0 +1,27 @@
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;
@RunWith(JUnit4.class)
public class ConsoleAppenderUsingPatternLayoutWithColorsTest {
@Rule
public LoggerContextRule contextRule = new LoggerContextRule("log4j2-console-appender_pattern-layout.xml");
@Test
public void givenLoggerWithConsoleConfig_shouldLogToConsoleInColors() throws Exception {
Logger logger = contextRule.getLogger(getClass().getSimpleName());
logger.trace("This is a colored message at TRACE level. This is the minimum visible level.");
logger.debug("This is a colored message at DEBUG level.");
logger.info("This is a colored message at INFO level.");
logger.warn("This is a colored message at WARN level.");
Exception e = new RuntimeException("This is only a test!");
logger.error("This is a colored message at ERROR level.", e);
logger.fatal("This is a colored message at FATAL level.");
}
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<File name="JSONLogfile" fileName="target/logfile.json">
<JSONLayout compact="true" eventEol="true"/>
</File>
<Async name="Async">
<AppenderRef ref="JSONLogfile"/>
</Async>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Async"/>
</Root>
</Loggers>
</Configuration>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" xmlns:xi="http://www.w3.org/2001/XInclude">
<Appenders>
<xi:include href="log4j2-includes/console-appender_pattern-layout_colored.xml"/>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%style{%date{DEFAULT}}{yellow} [%style{%thread}{white}] %highlight{%-5level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green, DEBUG=cyan, TRACE=blue} %style{%logger{36}}{cyan}:%n[+] %message%n%throwable"/>
</Console>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n%throwable"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

40
logging/pom.xml Normal file
View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.logging</groupId>
<artifactId>logging</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>log4j2</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>