Spring Boot登录到mysql数据库 [英] Spring boot logging into mysql database

查看:64
本文介绍了Spring Boot登录到mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将所有日志数据(即调试,信息,错误)放入mysql数据库,而不是文件/控制台.我阅读了spring boot文档,但没有看到与用于记录数据库的任何配置.

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

也尝试了以下链接,但也无法正常工作. https://www.tutorialspoint.com/log4j/log4j_logging_database.htm

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.

推荐答案

我阅读了spring boot文档,但没有看到任何内容与用于记录的数据库相关的配置.

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

因为spring boot将该功能移交给了日志记录框架(slf4j + logback/log4j等).因此,您需要使用其配置文件(例如:logback.xml,logback-spring.xml,logback.groovy等)相应地配置日志记录框架.Spring Boot中的默认日志记录框架是slf4j + logback.因此,请查看如何使用DBAppender.

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.

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

1.创建logback.xml文件:

<?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.创建3个表

logging_event

logging_event

logging_event_property

logging_event_property

logging_event_exception

logging_event_exception

它们必须存在,才能使用DBAppender

They must exist before DBAppender can be used

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

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

这篇关于Spring Boot登录到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆