6

I have to put all the log data (i.e., debug, info, error) into mysql database instead of to file/console. I read the spring boot documentation but I didn't see any configuration related to database for logging.

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

Also tried the following link but its also not working. https://www.tutorialspoint.com/log4j/log4j_logging_database.htm

Can anyone help me to do this. Thanks.

1 Answer 1

7

I read the spring boot documentation but I didn't see any configuration related to database for logging.

Because spring boot hands off that functionality to logging framework (slf4j + logback/log4j etc). So you need to configure your logging framework accordingly using it's configuration file (eg: logback.xml, logback-spring.xml, logback.groovy etc). Default logging frameworks in Spring boot is slf4j+logback. So checkout how you can use DBAppender.

For Logback

https://logback.qos.ch/manual/appenders.html#DBAppender http://learningviacode.blogspot.com/2014/01/writing-logs-to-database.html Log to database with LogBack https://medium.com/@chakrar27/storing-log-data-in-postgresql-using-logback-db-appender-292891a9918

1. create logback.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %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://localhost:5432/simple</url>
            <user>postgres</user>
            <password>root</password> <!-- no password -->
        </connectionSource>
    </appender>

    <!-- the level of the root level is set to DEBUG by default. -->
    <root level="TRACE">
        <appender-ref ref="stdout" />
        <appender-ref ref="db" />
    </root>
</configuration>

2. Create the 3 tables

logging_event

logging_event_property

logging_event_exception

They must exist before DBAppender can be used

For Log4J

https://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender

For Log4J2

http://smasue.github.io/log4j2-spring-database-appender

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. I have tried"Logback" now. Working fine with default tables but creating custom table/fields really hard.
Your connection string must be legitimate .. or you may be a "dialect" error. Make sure your connection string is correct/working before debugging an dialect issues. #learnedTheHardWay
Yes, customizing logback is much harder than other frameworks I've seen.
I found the script for the db tables in postgresql.sql of logback-classic-1.2.3.jar. Full folder path `logback-classic-1.2.3.jar!\ch\qos\logback\classic\db\script`
Should the logback.xml file be created in the resources folder or the project root directory?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.