parent
a929225e13
commit
0bd7a5d0ea
|
@ -38,6 +38,18 @@
|
|||
<version>2.8.2</version>
|
||||
</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. -->
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -4,13 +4,13 @@
|
|||
<File name="JSONLogfile" fileName="target/logfile.json">
|
||||
<JSONLayout compact="true" eventEol="true"/>
|
||||
</File>
|
||||
<Async name="Async">
|
||||
<Async name="AsyncAppender" bufferSize="80">
|
||||
<AppenderRef ref="JSONLogfile"/>
|
||||
</Async>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="trace">
|
||||
<AppenderRef ref="Async"/>
|
||||
<Root level="INFO">
|
||||
<AppenderRef ref="AsyncAppender"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
<xi:include href="log4j2-includes/console-appender_pattern-layout_colored.xml"/>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="trace">
|
||||
<AppenderRef ref="Console"/>
|
||||
<Root level="TRACE">
|
||||
<AppenderRef ref="ConsoleAppender"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
|
|
|
@ -6,15 +6,15 @@
|
|||
|
||||
<Syslog name="Syslog" host="localhost" port="514" protocol="TCP" ignoreExceptions="false"/>
|
||||
|
||||
<Failover name="Failover" primary="Syslog">
|
||||
<Failover name="FailoverAppender" primary="Syslog">
|
||||
<Failovers>
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="ConsoleAppender"/>
|
||||
</Failovers>
|
||||
</Failover>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="debug">
|
||||
<AppenderRef ref="Failover"/>
|
||||
<Root level="TRACE">
|
||||
<AppenderRef ref="FailoverAppender"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<?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"/>
|
||||
</Console>
|
||||
|
|
|
@ -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>
|
|
@ -1,7 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<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">
|
||||
<XMLLayout/>
|
||||
<Policies>
|
||||
|
@ -10,8 +11,8 @@
|
|||
</RollingFile>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="trace">
|
||||
<AppenderRef ref="XMLLogfile"/>
|
||||
<Root level="TRACE">
|
||||
<AppenderRef ref="XMLLogfileAppender"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<Console name="ConsoleAppender" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n%throwable"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="error">
|
||||
<AppenderRef ref="Console"/>
|
||||
<Root level="ERROR">
|
||||
<AppenderRef ref="ConsoleAppender"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
|
|
Loading…
Reference in New Issue