BAEL-41: Added including tests:

- added JDBC appender.
This commit is contained in:
Christian Rädel 2016-10-03 11:27:05 +02:00
parent a929225e13
commit 0bd7a5d0ea
10 changed files with 125 additions and 16 deletions

View File

@ -38,6 +38,18 @@
<version>2.8.2</version> <version>2.8.2</version>
</dependency> </dependency>
<!-- This is used by JDBC Appender. -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<!-- This is used for testing only. --> <!-- This is used for testing only. -->
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -4,13 +4,13 @@
<File name="JSONLogfile" fileName="target/logfile.json"> <File name="JSONLogfile" fileName="target/logfile.json">
<JSONLayout compact="true" eventEol="true"/> <JSONLayout compact="true" eventEol="true"/>
</File> </File>
<Async name="Async"> <Async name="AsyncAppender" bufferSize="80">
<AppenderRef ref="JSONLogfile"/> <AppenderRef ref="JSONLogfile"/>
</Async> </Async>
</Appenders> </Appenders>
<Loggers> <Loggers>
<Root level="trace"> <Root level="INFO">
<AppenderRef ref="Async"/> <AppenderRef ref="AsyncAppender"/>
</Root> </Root>
</Loggers> </Loggers>
</Configuration> </Configuration>

View File

@ -4,8 +4,8 @@
<xi:include href="log4j2-includes/console-appender_pattern-layout_colored.xml"/> <xi:include href="log4j2-includes/console-appender_pattern-layout_colored.xml"/>
</Appenders> </Appenders>
<Loggers> <Loggers>
<Root level="trace"> <Root level="TRACE">
<AppenderRef ref="Console"/> <AppenderRef ref="ConsoleAppender"/>
</Root> </Root>
</Loggers> </Loggers>
</Configuration> </Configuration>

View File

@ -6,15 +6,15 @@
<Syslog name="Syslog" host="localhost" port="514" protocol="TCP" ignoreExceptions="false"/> <Syslog name="Syslog" host="localhost" port="514" protocol="TCP" ignoreExceptions="false"/>
<Failover name="Failover" primary="Syslog"> <Failover name="FailoverAppender" primary="Syslog">
<Failovers> <Failovers>
<AppenderRef ref="Console"/> <AppenderRef ref="ConsoleAppender"/>
</Failovers> </Failovers>
</Failover> </Failover>
</Appenders> </Appenders>
<Loggers> <Loggers>
<Root level="debug"> <Root level="TRACE">
<AppenderRef ref="Failover"/> <AppenderRef ref="FailoverAppender"/>
</Root> </Root>
</Loggers> </Loggers>
</Configuration> </Configuration>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Console name="Console" target="SYSTEM_OUT"> <Console name="ConsoleAppender" 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"/> <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> </Console>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<JDBC name="JDBCAppender" tableName="logs">
<ConnectionFactory class="com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory"
method="getConnection"/>
<Column name="when" isEventTimestamp="true"/>
<Column name="logger" pattern="%logger"/>
<Column name="level" pattern="%level"/>
<Column name="message" pattern="%message"/>
<Column name="throwable" pattern="%ex{full}"/>
</JDBC>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="JDBCAppender"/>
</Root>
</Loggers>
</Configuration>

View File

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

View File

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