关于java:Spring Boot Logback DB Appender属性

Spring Boot Logback DB Appender Properties

嗨,我想在我的Spring Boot应用程序中使用DBAppender。 我想从application.properties文件中检索数据库连接属性。 但是,似乎无法识别它们。
请记住,我正在使用Spring Boot 1.2.x,所以我还不能使用logback-spring.xml

我正在使用的配置如下:

1
2
3
4
5
6
7
8
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">

            <driverClass>${spring.datasource.driver-class-name}</driverClass>
            <url>${spring.datasource.url}</url>
            <user>${spring.datasource.username}</user>
            <password>${spring.datasource.password}</password>
        </connectionSource>
    </appender>


在寻找类似解决方案时偶然发现了这一点。由于这仍然无法解决,因此我找到了以下几种方法:

1)如果您使用的是Spring Boot 1.3+(您已经指出您不是为了将来参考),我设法使用了标签来重用application.properties中的相同值。

application.properties(用于嵌入式H2 DB):

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

logback-spring.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
<springProperty name="spring.datasource.driverClassName" source="spring.datasource.driverClassName"/>
<springProperty name="spring.datasource.url" source="spring.datasource.url"/>
<springProperty name="spring.datasource.username" source="spring.datasource.username"/>
<springProperty name="spring.datasource.password" source="spring.datasource.password"/>


    <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
        <driverClass>${spring.datasource.driverClassName}</driverClass>
        <url>${spring.datasource.url}</url>
        <user>${spring.datasource.username}</user>
        <password>${spring.datasource.password}</password>
    </connectionSource>
</appender>

2)导入应用程序属性作为属性源:无法在logback.xml中使用Spring属性占位符

1
<property resource="application.properties" />

3)也许您可以在容器JNDI中注册数据源并改用logback的JNDIConnectionSource?看看其他文章:如何在带有嵌入式Tomcat容器的Spring Boot中创建JNDI上下文


因此,请支持jpt的答案。
因为没有"

"(或该答案中的.properties),我什么都不会工作。

但我的回答和贡献:我想添加此内容,也可以与application.yml一起使用。

我将尝试列出我在这里所做的一切:

设置3个环境变量。

1
2
3
SPRING_DATASOURCE_URL
SPRING_DATASOURCE_USER
SPRING_DATASOURCE_PASSWORD

application.yml的内容(如下)

1
2
3
4
5
6
7
8
9
spring:
  datasource:
    #SPRING_DATASOURCE_URL environment variable will be something like -> jdbc:sqlserver://MySqlServer\\\\MyInstance:1433;DatabaseName=MyDbName;
    url: ${SPRING_DATASOURCE_URL}
    username: ${SPRING_DATASOURCE_USERNAME}
    password: ${SPRING_DATASOURCE_PASSWORD}
    driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
logging:
    config: classpath:logback-spring.xml

注意,我正在使用文件" logback-spring.xml"。我不确定是否会有所作为(与仅使用" logback.xml"不同)

logback-spring.xml的内容(如下)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<configuration debug="true" scan="true" scanPeriod="30 seconds">


   
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] %msg%n</pattern>
        </encoder>
    </appender>



    <!-- THIS IS THE MAGIC LINE that JPT figured out -->
    <property resource="application.yml" />

    <springProperty name="humptydumptyurl" source="spring.datasource.url"/>
    <springProperty name="humptydumptyusername" source="spring.datasource.username"/>
    <springProperty name="humptydumptypassword" source="spring.datasource.password"/>


   
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
            <driverClass>com.microsoft.sqlserver.jdbc.SQLServerDriver</driverClass>
            <url>${humptydumptyurl}</url>
            <user>${humptydumptyusername}</user>
            <password>${humptydumptypassword}</password>
        </connectionSource>
    </appender>



    <root level="INFO">
       
       
    </root>

</configuration>

注意,我故意使用" humptydumpty"以避免歧义。您可能会使用更好的前缀,但我想显示映射的左侧可以是您想要的任何名称。

注意,我的application.properties文件为空。我是100%application.yml。

注意,您还可以设置/使用第四个环境变量(我认为它可以工作,但我尚未测试)

SPRING_DATASOURCE_DRIVER-CLASS-NAME

和它的两个伙伴

1
2
datasource:
    driverClassName: ${SPRING_DATASOURCE_DRIVER-CLASS-NAME}

1
  <springProperty name="humptydumptydriverclassname" source="spring.datasource.driver-class-name"/>

......

以下一些mssql-server注释(对于大多数用户而言可能并不重要)

我用了

1
2
3
4
5
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            mssql-jdbc</artifactId>
            <version>7.0.0.jre8</version>
        </dependency>

并使其正常工作。

我必须对DDL进行一些调整,因为在INSERT上出现"截断"错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- This SQL script creates the required tables by ch.qos.logback.classic.db.DBAppender
--
-- The event_id column type was recently changed from INT to DECIMAL(40)
-- without testing.

DROP TABLE logging_event_property
DROP TABLE logging_event_exception
DROP TABLE logging_event

CREATE TABLE logging_event
  (
    timestmp         DECIMAL(20) NOT NULL,
    formatted_message  VARCHAR(max) NOT NULL,
    logger_name       VARCHAR(512) NOT NULL,
    level_string      VARCHAR(512) NOT NULL,
    thread_name       VARCHAR(512),
    reference_flag    SMALLINT,
    arg0              VARCHAR(512),
    arg1              VARCHAR(512),
    arg2              VARCHAR(512),
    arg3              VARCHAR(512),
    caller_filename   VARCHAR(512) NOT NULL,
    caller_class      VARCHAR(512) NOT NULL,
    caller_method     VARCHAR(512) NOT NULL,
    caller_line       CHAR(16) NOT NULL,
    event_id          DECIMAL(38) NOT NULL identity,
    PRIMARY KEY(event_id)
  )

CREATE TABLE logging_event_property
  (
    event_id          DECIMAL(38) NOT NULL,
    mapped_key        VARCHAR(512) NOT NULL,
    mapped_value      VARCHAR(1024),
    PRIMARY KEY(event_id, mapped_key),
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
  )

CREATE TABLE logging_event_exception
  (
    event_id         DECIMAL(38) NOT NULL,
    i                SMALLINT NOT NULL,
    trace_line       VARCHAR(512) NOT NULL,
    PRIMARY KEY(event_id, i),
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
  )

最后,让我给出所有最重要的调试提示。

首先将您的连接字符串硬编码为值,使其正常工作...开始插入环境变量替代项时。

ch.qos.logback.classic.db.DBAppender必须具有良好且有效的连接字符串才能执行"检查" .....如果输入错误的连接字符串,则会出现此类错误(如下) 。我花了4个小时来追查错误,这是因为追逐以下错误而发生的,仅仅是因为我的连接字符串无法正常工作。同样,首先将正确/有效的值硬编码到logback-spring.xml中,使其生效,然后回旋并执行环境变量spring-properties替换伏都教。

因此,对于db-appender,首先将您的URL,用户名,密码硬编码,使其正常运行,然后慢慢开始进行替换...我在发现所有替换均不起作用之前就追逐了以下错误方式。 ..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
06:19:09,721 |-WARN in ch.qos.logback.classic.db.DBAppender[MyDbAppender] - Attempted to append to non started appender [MyDbAppender].
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:202)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:75)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:347)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
Caused by: java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter@68:16 - RuntimeException in Action for tag [appender] java.lang.IllegalStateException: DBAppender cannot function if the JDBC driver does not support getGeneratedKeys method *and* without a specific SQL dialect
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:169)
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:67)
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:57)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:117)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:298)

并仅列出所有内容。

这些是我使用的登录版本:

1
2
3
4
5
6
7
8
9
10
        <dependency>
            <groupId>ch.qos.logback</groupId>
            logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>