diff --git a/logging/log4j2/pom.xml b/logging/log4j2/pom.xml new file mode 100644 index 0000000000..537427e232 --- /dev/null +++ b/logging/log4j2/pom.xml @@ -0,0 +1,58 @@ + + + + logging + com.baeldung.logging + 1.0.0-SNAPSHOT + + 4.0.0 + + log4j2 + + + + + org.apache.logging.log4j + log4j-api + 2.6.2 + + + org.apache.logging.log4j + log4j-core + 2.6.2 + + + + + com.fasterxml.jackson.core + jackson-databind + 2.8.2 + + + + + org.apache.logging.log4j + log4j-core + 2.6.2 + test-jar + test + + + junit + junit + 4.12 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + diff --git a/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/AsyncFileAppenderUsingJsonLayoutTest.java b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/AsyncFileAppenderUsingJsonLayoutTest.java new file mode 100644 index 0000000000..c9ce0c2828 --- /dev/null +++ b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/AsyncFileAppenderUsingJsonLayoutTest.java @@ -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 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); + } +} diff --git a/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingDefaultLayoutTest.java b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingDefaultLayoutTest.java new file mode 100644 index 0000000000..9d8b2ae81b --- /dev/null +++ b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingDefaultLayoutTest.java @@ -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); + } +} diff --git a/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java new file mode 100644 index 0000000000..c8d017122e --- /dev/null +++ b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java @@ -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."); + } +} diff --git a/logging/log4j2/src/test/resources/log4j2-async-file-appender_json-layout_colored.xml b/logging/log4j2/src/test/resources/log4j2-async-file-appender_json-layout_colored.xml new file mode 100644 index 0000000000..74767cfb0d --- /dev/null +++ b/logging/log4j2/src/test/resources/log4j2-async-file-appender_json-layout_colored.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/logging/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml b/logging/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml new file mode 100644 index 0000000000..2c6f6a1ed0 --- /dev/null +++ b/logging/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/logging/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml b/logging/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml new file mode 100644 index 0000000000..02f75be4de --- /dev/null +++ b/logging/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml @@ -0,0 +1,4 @@ + + + + diff --git a/logging/log4j2/src/test/resources/log4j2.xml b/logging/log4j2/src/test/resources/log4j2.xml new file mode 100644 index 0000000000..3f214e230a --- /dev/null +++ b/logging/log4j2/src/test/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/logging/pom.xml b/logging/pom.xml new file mode 100644 index 0000000000..535c9e1c54 --- /dev/null +++ b/logging/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + com.baeldung.logging + logging + 1.0.0-SNAPSHOT + + log4j2 + + pom + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + .. + + + + UTF-8 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + +