Spring Boot Logback DB Appender Properties

后端 未结 2 652
[愿得一人]
[愿得一人] 2020-12-21 18:44

Hi I want to use a DBAppenderin my Spring Boot application. I want to retrieve database connection properties from the application.properties file.

2条回答
  •  青春惊慌失措
    2020-12-21 19:17

    Stumbled upon this while searching for a similar solution. Since this is still unanswered, here's a few approaches I found:

    1) If you are using Spring Boot 1.3+ (which you already pointed out you're not but for future reference), I managed to use the tag to reuse the same values from application.properties.

    application.properties (for embedded H2 DB):

    spring.datasource.driverClassName=org.h2.Driver
    spring.datasource.url=jdbc:h2:mem:testdb
    spring.datasource.username=sa
    spring.datasource.password=
    

    logback-spring.xml:

    
    
    
    
    
    
        
            ${spring.datasource.driverClassName}
            ${spring.datasource.url}
            ${spring.datasource.username}
            ${spring.datasource.password}
        
    
    

    2) Import application properties as property source: Unable to use Spring Property Placeholders in logback.xml

    
    

    3) Maybe you're able to register the Datasource in the container JNDI and use logback's JNDIConnectionSource instead? Check out this other post: How to create JNDI context in Spring Boot with Embedded Tomcat Container

提交回复
热议问题