Spring boot logging into mysql database

后端 未结 1 1297
故里飘歌
故里飘歌 2021-02-10 08:14

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 configurati

1条回答
  •  無奈伤痛
    2021-02-10 08:46

    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:

    
    
        
            
            
                %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
                
            
        
        
            
                org.postgresql.Driver
                jdbc:postgresql://localhost:5432/simple
                postgres
                root 
            
        
    
        
        
            
            
        
    
    

    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

    0 讨论(0)
提交回复
热议问题