Search code examples
springspring-bootlogbackslf4j

Log to database using slf4j with Spring Boot


I'm trying to log messages to Postgresql database. I used this script to create all the tables on Postgres

Below is my logback.xml file

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT"
        class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread])
                %highlight(%-5level) %logger{36}.%M - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="db"
        class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource
            class="ch.qos.logback.core.db.DriverManagerConnectionSource">
            <driverClass>org.postgresql.Driver</driverClass>
            <url>jdbc:postgresql://hanno.db.elephantsql.com:5432/MYDATABASE</url>
            <user>MYUSERNAME</user>
            <password>MYPASS</password>
        </connectionSource>
    </appender>
    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

In my application I have the following code for testing

LOGGER.error("Error", "dd");

I do see the error message on my console. But nothing gets stored in the database. Any idea what I have done wrong?


Solution

  • This line is caused that:

    <appender-ref ref="STDOUT" />
    

    Replace "STDOUT" to "db" to use a right appender.