3
3
.
.
1
1
.
.
5
5
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
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.
It seams that the same can't be achieved through the application.properties alone.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
hello()
MyController
http://localhost:8080/Hello
Browser
logback.xml
Create Tables
(SQL)
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 DB source)
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)
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" />
<!-- APPENDER -->
<appender name="MyDBAppenderName" class="ch.qos.logback.classic.db.DBAppender">
<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.controllers" level="INFO">
<appender-ref ref="MyDBAppenderName"/>
</logger>
</configuration>
Create Tables SQL
DROP TABLE logging_event_property;
DROP TABLE logging_event_exception;
DROP TABLE logging_event;
DROP SEQUENCE logging_event_id_seq;
CREATE SEQUENCE logging_event_id_seq MINVALUE 1 START 1;
CREATE TABLE logging_event
(
timestmp BIGINT NOT NULL,
formatted_message TEXT NOT NULL,
logger_name VARCHAR(254) NOT NULL,
level_string VARCHAR(254) NOT NULL,
thread_name VARCHAR(254),
reference_flag SMALLINT,
arg0 VARCHAR(254),
arg1 VARCHAR(254),
arg2 VARCHAR(254),
arg3 VARCHAR(254),
caller_filename VARCHAR(254) NOT NULL,
caller_class VARCHAR(254) NOT NULL,
caller_method VARCHAR(254) NOT NULL,
caller_line CHAR(4) NOT NULL,
event_id BIGINT DEFAULT nextval('logging_event_id_seq') PRIMARY KEY
);
CREATE TABLE logging_event_property
(
event_id BIGINT NOT NULL,
mapped_key VARCHAR(254) NOT NULL,
mapped_value VARCHAR(1024),
PRIMARY KEY(event_id, mapped_key),
FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
);
CREATE TABLE logging_event_exception
(
event_id BIGINT NOT NULL,
i SMALLINT NOT NULL,
trace_line VARCHAR(254) NOT NULL,
PRIMARY KEY(event_id, i),
FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
);
MyController.java
package com.ivoronline.springboot_log_logback_config_xml_db.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";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
pgAdmin4
SQL (IntelliJ Database Console)
SELECT timestmp, formatted_message, level_string, caller_filename, caller_method, caller_line
FROM public.logging_event
logging_event (IntelliJ Database Tool)
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>