diff --git a/log4j2/pom.xml b/log4j2/pom.xml index 893c79be72..a4c8f19f69 100644 --- a/log4j2/pom.xml +++ b/log4j2/pom.xml @@ -1,105 +1,104 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - log4j2 + log4j2 - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - .. - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + .. + - - - - org.apache.logging.log4j - log4j-core - ${log4j-core.version} - + + + + org.apache.logging.log4j + log4j-core + ${log4j-core.version} + - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml - ${jackson.version} - + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + - - - com.h2database - h2 - ${h2.version} - - - org.apache.commons - commons-dbcp2 - ${commons-dbcp2.version} - + + + com.h2database + h2 + ${h2.version} + + + org.apache.commons + commons-dbcp2 + ${commons-dbcp2.version} + - - - org.apache.logging.log4j - log4j-core - ${log4j-core.version} - test-jar - test - - - junit - junit - ${junit.version} - test - - + + + org.apache.logging.log4j + log4j-core + ${log4j-core.version} + test-jar + test + + + junit + junit + ${junit.version} + test + + - - + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - **/*IntegrationTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - - true - - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + true + + - - + + - - 2.8.5 - 1.4.193 - 2.1.1 - 2.7 - 4.12 - - 3.6.0 - 2.19.1 - + + 2.8.5 + 1.4.193 + 2.1.1 + 2.7 + 4.12 + 3.6.0 + 2.19.1 + diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/AsyncFileAppenderUsingJsonLayoutTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/AsyncFileAppenderUsingJsonLayoutTest.java deleted file mode 100644 index 0472c2219e..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/AsyncFileAppenderUsingJsonLayoutTest.java +++ /dev/null @@ -1,32 +0,0 @@ -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 static org.junit.Assert.assertTrue; - -@RunWith(JUnit4.class) -public class AsyncFileAppenderUsingJsonLayoutTest { - @Rule - public LoggerContextRule contextRule = - new LoggerContextRule("log4j2-async-file-appender_json-layout.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); - } - long logEventsCount = Files.lines(Paths.get("target/logfile.json")).count(); - assertTrue(logEventsCount > 0 && logEventsCount <= count); - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingDefaultLayoutTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingDefaultLayoutTest.java deleted file mode 100644 index 9831030d02..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingDefaultLayoutTest.java +++ /dev/null @@ -1,21 +0,0 @@ -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(getClass()); - Exception e = new RuntimeException("This is only a test!"); - logger.info("This is a simple message at INFO level. " + - "It will be hidden."); - logger.error("This is a simple message at ERROR level. " + - "This is the minimum visible level.", e); - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java deleted file mode 100644 index 86b005538f..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.logging.log4j2.tests; - -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.MarkerManager; -import org.apache.logging.log4j.ThreadContext; -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."); - logger.debug("This is a colored message at DEBUG level. " + - "This is the minimum visible 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."); - } - - @Test - public void givenLoggerWithConsoleConfig_shouldFilterByMarker() throws Exception { - Logger logger = contextRule.getLogger("ConnTrace"); - Marker appError = MarkerManager.getMarker("APP_ERROR"); - logger.error(appError, "This marker message at ERROR level should be hidden."); - Marker connectionTrace = MarkerManager.getMarker("CONN_TRACE"); - logger.trace(connectionTrace, "This is a marker message at TRACE level."); - } - - @Test - public void givenLoggerWithConsoleConfig_shouldFilterByThreadContext() throws Exception { - Logger logger = contextRule.getLogger("UserAudit"); - ThreadContext.put("userId", "1000"); - logger.info("This is a log-visible user login. Maybe from an admin account?"); - ThreadContext.put("userId", "1001"); - logger.info("This is a log-invisible user login."); - boolean b = true; - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java new file mode 100644 index 0000000000..d88a967562 --- /dev/null +++ b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java @@ -0,0 +1,121 @@ +package com.baeldung.logging.log4j2.tests; + +import static org.junit.Assert.assertTrue; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.ResultSet; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; +import org.apache.logging.log4j.ThreadContext; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory; + +@RunWith(JUnit4.class) +public class CustomLoggingTest { + + @BeforeClass + public static void setup() throws Exception { + Connection connection = ConnectionFactory.getConnection(); + connection.createStatement() + .execute("CREATE TABLE logs(" + "when TIMESTAMP," + "logger VARCHAR(255)," + "level VARCHAR(255)," + "message VARCHAR(4096)," + "throwable TEXT)"); + connection.commit(); + } + + @Test + public void givenLoggerWithDefaultConfig_shouldLogToConsole() throws Exception { + Logger logger = LogManager.getLogger(getClass()); + Exception e = new RuntimeException("This is only a test!"); + logger.info("This is a simple message at INFO level. " + "It will be hidden."); + logger.error("This is a simple message at ERROR level. " + "This is the minimum visible level.", e); + } + + @Test + public void givenLoggerWithConsoleConfig_shouldLogToConsoleInColors() throws Exception { + Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER"); + logger.trace("This is a colored message at TRACE level."); + logger.debug("This is a colored message at DEBUG level. " + "This is the minimum visible 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."); + } + + @Test + public void givenLoggerWithConsoleConfig_shouldFilterByMarker() throws Exception { + Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER"); + Marker appError = MarkerManager.getMarker("APP_ERROR"); + logger.error(appError, "This marker message at ERROR level should be hidden."); + Marker connectionTrace = MarkerManager.getMarker("CONN_TRACE"); + logger.trace(connectionTrace, "This is a marker message at TRACE level."); + } + + @Test + public void givenLoggerWithConsoleConfig_shouldFilterByThreadContext() throws Exception { + Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_THREAD_CONTEXT"); + ThreadContext.put("userId", "1000"); + logger.info("This is a log-visible user login. Maybe from an admin account?"); + ThreadContext.put("userId", "1001"); + logger.info("This is a log-invisible user login."); + + } + + @Test + public void givenLoggerWithAsyncConfig_shouldLogToJsonFile() throws Exception { + Logger logger = LogManager.getLogger("ASYNC_JSON_FILE_APPENDER"); + final int count = 88; + for (int i = 0; i < count; i++) { + logger.info("This is async JSON message #{} at INFO level.", count); + } + long logEventsCount = Files.lines(Paths.get("target/logfile.json")) + .count(); + assertTrue(logEventsCount > 0 && logEventsCount <= count); + } + + @Test + public void givenLoggerWithFailoverConfig_shouldLog() throws Exception { + Logger logger = LogManager.getLogger("FAIL_OVER_SYSLOG_APPENDER"); + logger.trace("This is a syslog message at TRACE level."); + logger.debug("This is a syslog message at DEBUG level."); + logger.info("This is a syslog message at INFO level. This is the minimum visible level."); + logger.warn("This is a syslog message at WARN level."); + Exception e = new RuntimeException("This is only a test!"); + logger.error("This is a syslog message at ERROR level.", e); + logger.fatal("This is a syslog message at FATAL level."); + } + + @Test + public void givenLoggerWithJdbcConfig_shouldLogToDataSource() throws Exception { + Logger logger = LogManager.getLogger("JDBC_APPENDER"); + final int count = 88; + for (int i = 0; i < count; i++) { + logger.info("This is JDBC message #{} at INFO level.", count); + } + Connection connection = ConnectionFactory.getConnection(); + ResultSet resultSet = connection.createStatement() + .executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs"); + int logCount = 0; + if (resultSet.next()) { + logCount = resultSet.getInt("ROW_COUNT"); + } + assertTrue(logCount == count); + } + + @Test + public void givenLoggerWithRollingFileConfig_shouldLogToXMLFile() throws Exception { + Logger logger = LogManager.getLogger("XML_ROLLING_FILE_APPENDER"); + final int count = 88; + for (int i = 0; i < count; i++) { + logger.info("This is rolling file XML message #{} at INFO level.", i); + } + } + +} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/FailoverSyslogConsoleAppenderTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/FailoverSyslogConsoleAppenderTest.java deleted file mode 100644 index 0653394e5a..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/FailoverSyslogConsoleAppenderTest.java +++ /dev/null @@ -1,27 +0,0 @@ -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 FailoverSyslogConsoleAppenderTest { - @Rule - public LoggerContextRule contextRule = - new LoggerContextRule("log4j2-failover-syslog-console-appender_pattern-layout.xml"); - - @Test - public void givenLoggerWithFailoverConfig_shouldLog() throws Exception { - Logger logger = contextRule.getLogger(getClass().getSimpleName()); - logger.trace("This is a syslog message at TRACE level."); - logger.debug("This is a syslog message at DEBUG level."); - logger.info("This is a syslog message at INFO level. This is the minimum visible level."); - logger.warn("This is a syslog message at WARN level."); - Exception e = new RuntimeException("This is only a test!"); - logger.error("This is a syslog message at ERROR level.", e); - logger.fatal("This is a syslog message at FATAL level."); - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JDBCAppenderTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JDBCAppenderTest.java deleted file mode 100644 index 1b8d33e2bf..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JDBCAppenderTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.logging.log4j2.tests; - -import com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.junit.LoggerContextRule; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import java.sql.Connection; -import java.sql.ResultSet; - -import static org.junit.Assert.assertTrue; - -@RunWith(JUnit4.class) -public class JDBCAppenderTest { - @Rule - public LoggerContextRule contextRule = new LoggerContextRule("log4j2-jdbc-appender.xml"); - - @BeforeClass - public static void setup() throws Exception { - Connection connection = ConnectionFactory.getConnection(); - connection.createStatement() - .execute("CREATE TABLE logs(" + - "when TIMESTAMP," + - "logger VARCHAR(255)," + - "level VARCHAR(255)," + - "message VARCHAR(4096)," + - "throwable TEXT)"); - //connection.commit(); - } - - @Test - public void givenLoggerWithJdbcConfig_shouldLogToDataSource() throws Exception { - Logger logger = contextRule.getLogger(getClass().getSimpleName()); - final int count = 88; - for (int i = 0; i < count; i++) { - logger.info("This is JDBC message #{} at INFO level.", count); - } - Connection connection = ConnectionFactory.getConnection(); - ResultSet resultSet = connection.createStatement() - .executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs"); - int logCount = 0; - if (resultSet.next()) { - logCount = resultSet.getInt("ROW_COUNT"); - } - assertTrue(logCount == count); - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/RollingFileAppenderUsingXMLLayoutTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/RollingFileAppenderUsingXMLLayoutTest.java deleted file mode 100644 index 3ab69d263c..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/RollingFileAppenderUsingXMLLayoutTest.java +++ /dev/null @@ -1,34 +0,0 @@ -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.", i); - } - String[] logEvents = Files.readAllLines(Paths.get("target/logfile.xml")).stream() - .collect(Collectors.joining(System.lineSeparator())) - .split("\\n\\n+"); - assertTrue(logEvents.length == 39); - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java index 73b323f335..cc594f293c 100644 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java +++ b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java @@ -2,7 +2,6 @@ package com.baeldung.logging.log4j2.tests.jdbc; import org.apache.commons.dbcp2.BasicDataSource; import org.h2.Driver; - import java.sql.Connection; import java.sql.SQLException; diff --git a/log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml b/log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml deleted file mode 100644 index c291eacd59..0000000000 --- a/log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml b/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml deleted file mode 100644 index d6621f9166..0000000000 --- a/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml b/log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml deleted file mode 100644 index 62ba37f28c..0000000000 --- a/log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml b/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml index c2b9c65430..fd61e4581f 100644 --- a/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml +++ b/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml @@ -1,4 +1,5 @@ - + diff --git a/log4j2/src/test/resources/log4j2-jdbc-appender.xml b/log4j2/src/test/resources/log4j2-jdbc-appender.xml deleted file mode 100644 index 6b50f7d5a4..0000000000 --- a/log4j2/src/test/resources/log4j2-jdbc-appender.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml b/log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml deleted file mode 100644 index 9de1a29186..0000000000 --- a/log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/log4j2/src/test/resources/log4j2.xml b/log4j2/src/test/resources/log4j2.xml index 8f7608aa78..83c1184f1f 100644 --- a/log4j2/src/test/resources/log4j2.xml +++ b/log4j2/src/test/resources/log4j2.xml @@ -1,13 +1,69 @@ - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + - + \ No newline at end of file