3
3
.
.
1
1
.
.
6
6
L
L
o
o
g
g
b
b
a
a
c
c
k
k
-
-
C
C
o
o
n
n
f
f
i
i
g
g
u
u
r
r
e
e
-
-
X
X
M
M
L
L
-
-
D
D
B
B
-
-
C
C
u
u
s
s
t
t
o
o
m
m
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to use XML configuration to configure Logback to log into PostgreSQL Database.
We will use MyDBAppender.java to define custom table and columns to store log events.
DB connection parameters need to be defined inside application.properties or application will not work.
To prevent typing the same parameters again in the logback.xml we will reference those from application.properties.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
SQL
Spring Data JPA
Enables: @Entity, @Id
SQL
PostgreSQL Database
Enables Hibernate to work with PostgreSQL DB
hello()
MyController
http://localhost:8080/Hello
Browser
logback.xml
Create Table
(SQL)
MyDBAppender
application.properties
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_log_logback_config_xml (add Spring Boot Starters from the table)
Edit File: application.properties (add Database connection parameters)
Create File: logback.xml (inside Directory resources)
Execute SQL: SQL Create Tables (manually create DB Tables in PostgreSQL DB)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside controllers package)
Create Package: log (inside log package)
Create Class: MyDBAppender.java (inside controllers package)
application.properties
# POSTGRESQL DATABASE
spring.datasource.url = jdbc:postgresql://localhost:5432/Test
spring.datasource.username = postgres
spring.datasource.password = letmein
spring.datasource.driver-class-name = org.postgresql.Driver
# JPA / HIBERNATE
spring.jpa.hibernate.ddl-auto = create-drop
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- IMPORT PROPERTIES -->
<property resource="application.properties" />
<!-- MY DB APPENDER -->
<appender name="MyDBAppenderName"
class="com.ivoronline.springboot_log_logback_config_xml_db_custom.log.MyDBAppender">
<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<url> ${spring.datasource.url} </url>
<user> ${spring.datasource.username} </user>
<password> ${spring.datasource.password} </password>
<driverClass> ${spring.datasource.driver-class-name} </driverClass>
</connectionSource>
</appender>
<!-- LOGGER -->
<logger name="com.ivoronline.springboot_log_logback_config_xml_db_custom.controllers" level="INFO">
<appender-ref ref="MyDBAppenderName"/>
</logger>
</configuration>
Create Table SQL
-- CREATE SEQUENCE
create sequence transaction_event_id_seq;
-- CREATE TABLE
create table transaction (
event_id serial DEFAULT nextval('transaction_event_id_seq'::regclass),
message varchar,
level varchar,
date timestamp
);
MyDBAppender.java
package com.ivoronline.springboot_log_logback_config_xml_db_custom.log;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import ch.qos.logback.classic.db.DBAppender;
import ch.qos.logback.classic.spi.ILoggingEvent;
public class MyDBAppender extends DBAppender {
//=======================================================================================
// GET INSERT SQL
//=======================================================================================
@Override
protected String getInsertSQL() {
return "INSERT INTO TRANSACTION (event_id, timestamp, level, message) VALUES (DEFAULT, ?, ?, ?)";
}
//=======================================================================================
// SUB APPEND
//=======================================================================================
@Override
protected void subAppend(ILoggingEvent event, Connection connection, PreparedStatement preparedStatement)
throws SQLException {
//GET LOG EVENT DATA
String level = event.getLevel().toString();
String message = event.getMessage();
//PREPARE STATEMENT
preparedStatement.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now()));
preparedStatement.setString (2, level );
preparedStatement.setString (3, message);
//EXECUTE PREPARED STATEMENT
preparedStatement.executeUpdate();
}
}
MyController.java
package com.ivoronline.springboot_log_logback_config_xml_db_custom.controllers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
Logger log = LoggerFactory.getLogger(MyController.class);
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
log.error("Some error occured");
log.warn ("Some warn occured");
log.info ("Some info occured");
log.debug("Some debug occured");
log.trace("Some trace occured");
return "Hello from Controller";
}
}