Introduction

During this exercise we will start logging to H2 Database

Background info

Some definitions
  • Know that SLF4J is the de-facto standard for generating logging with Spring Boot

  • Know that the physical logging is decoupled from SLF4J

  • Know that there are several physical logging frameworks

    • Log4J(2)

    • Logback

    • Java.util.Logging

Exercise: Logging to H2

Add dependencies

Add dependency to H2 (if not already is in your pom.xml)
   <dependency>
             <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
            <scope>runtime</scope>
        <dependency>

Add logback.xml

Add logback-spring .xml to src/main/resources
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
       <include resource="org/springframework/boot/logging/logback/base.xml"/>

        <springProperty name="spring.datasource.driver-class-name" source="spring.datasource.driverClassName"/>
        <springProperty name="spring.datasource.url" source="spring.datasource.url"/>
        <springProperty name="spring.datasource.username" source="spring.datasource.username"/>
        <springProperty name="spring.datasource.password" source="spring.datasource.password"/>

        <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
                <connectionSource
                        class="ch.qos.logback.core.db.DriverManagerConnectionSource">
                        <driverClass>${spring.datasource.driver-class-name}</driverClass>
                        <url>${spring.datasource.url}</url>
                        <user>${spring.datasource.username}</user>
                        <password>${spring.datasource.password}</password>
                </connectionSource>
        </appender>

    <root level="info">
                <appender-ref ref="DB" />
                <appender-ref ref="FILE" />
        <appender-ref ref="CONSOLE" />
    </root>

</configuration>

Create tables

Open with your web browser http://localhost:8080/h2-console and execute the following script

Create tables using this sql script
BEGIN;
DROP TABLE IF EXISTS logging_event_property;
DROP TABLE IF EXISTS logging_event_exception;
DROP TABLE IF EXISTS logging_event;
COMMIT;


BEGIN;
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 NOT NULL AUTO_INCREMENT PRIMARY KEY
  );
COMMIT;

BEGIN;
CREATE TABLE logging_event_property
  (
    event_id              BIGINT NOT NULL,
    mapped_key        VARCHAR(254) NOT NULL,
    mapped_value      TEXT,
    PRIMARY KEY(event_id, mapped_key),
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
  );
COMMIT;

BEGIN;
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)
  );
COMMIT;

Test your application

See that logging is now inserted into the qnh_logging database tables!!!

More info