diff --git a/logging/log4j2/pom.xml b/logging/log4j2/pom.xml index 01cdfc5872..d386836b6c 100644 --- a/logging/log4j2/pom.xml +++ b/logging/log4j2/pom.xml @@ -38,6 +38,18 @@ 2.8.2 + + + com.h2database + h2 + 1.4.192 + + + org.apache.commons + commons-dbcp2 + 2.1.1 + + org.apache.logging.log4j diff --git a/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JDBCAppenderTest.java b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JDBCAppenderTest.java new file mode 100644 index 0000000000..2ceda2083b --- /dev/null +++ b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JDBCAppenderTest.java @@ -0,0 +1,52 @@ +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/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java new file mode 100644 index 0000000000..73b323f335 --- /dev/null +++ b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java @@ -0,0 +1,25 @@ +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; + +public class ConnectionFactory { + private interface Singleton { + ConnectionFactory INSTANCE = new ConnectionFactory(); + } + + private BasicDataSource dataSource; + + private ConnectionFactory() { + dataSource = new BasicDataSource(); + dataSource.setDriver(new Driver()); + dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1"); + } + + public static Connection getConnection() throws SQLException { + return Singleton.INSTANCE.dataSource.getConnection(); + } +} diff --git a/logging/log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml b/logging/log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml index 74767cfb0d..eb338dd496 100644 --- a/logging/log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml +++ b/logging/log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml @@ -4,13 +4,13 @@ - + - - + + 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 index 2c6f6a1ed0..4167fb9366 100644 --- a/logging/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml +++ b/logging/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml @@ -4,8 +4,8 @@ - - + + diff --git a/logging/log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml b/logging/log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml index efb4516489..62ba37f28c 100644 --- a/logging/log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml +++ b/logging/log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml @@ -6,15 +6,15 @@ - + - + - - + + 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 index 02f75be4de..c2b9c65430 100644 --- 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 @@ -1,4 +1,4 @@ - + diff --git a/logging/log4j2/src/test/resources/log4j2-jdbc-appender.xml b/logging/log4j2/src/test/resources/log4j2-jdbc-appender.xml new file mode 100644 index 0000000000..6b50f7d5a4 --- /dev/null +++ b/logging/log4j2/src/test/resources/log4j2-jdbc-appender.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + diff --git a/logging/log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml b/logging/log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml index 8d47061a9e..059007f660 100644 --- a/logging/log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml +++ b/logging/log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml @@ -1,7 +1,8 @@ - @@ -10,8 +11,8 @@ - - + + diff --git a/logging/log4j2/src/test/resources/log4j2.xml b/logging/log4j2/src/test/resources/log4j2.xml index 3f214e230a..8f7608aa78 100644 --- a/logging/log4j2/src/test/resources/log4j2.xml +++ b/logging/log4j2/src/test/resources/log4j2.xml @@ -1,13 +1,13 @@ - + - - + +