我可以阻止 logback 的 DBAppender 记录特定属性吗?

发布于 2024-08-28 16:33:25 字数 2183 浏览 4 评论 0原文

Logback 的 DBAppender 将其上下文和 MDC 中的所有属性记录到数据库中。我想控制记录哪些属性,特别是过滤掉某些值,但我找不到任何选项来执行此操作。该文档很简洁:

logging_event_property用于存储MDC或Context中包含的键和值

是否可以排除某些属性被记录?

下面是一个示例:

配置了一个从 vct.properties 加载其属性的 DBAppender

<configuration>
    <property resource="vct.properties" />

    <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
            <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <driverClass>com.mysql.jdbc.Driver</driverClass>
                <jdbcUrl>jdbc:mysql://${log.db.host}:${log.db.port}/${log.db.schema}</jdbcUrl>
                <user>${log.db.username}</user>
                <password>${log.db.password}</password>
            </dataSource>
        </connectionSource>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="DB" />
    </root>
</configuration>

vct.properties 具有连接设置:

log.db.host=localhost
log.db.port=3306
log.db.schema=logs_development
log.db.username=loguser
log.db.password=logpass

Logback 记录事件时,会记录所有连接设置:

mysql> select * from logging_event_property where event_id=1;
+----------+---------------------+-------------------------------------------+
| event_id | mapped_key          | mapped_value                              |
+----------+---------------------+-------------------------------------------+
|        1 | log.db.host         | localhost                                 | 
|        1 | log.db.password     | logpass                                   | 
|        1 | log.db.port         | 3306                                      | 
|        1 | log.db.schema       | logs_development                          | 
|        1 | log.db.username     | loguser                                   | 
+----------+---------------------+-------------------------------------------+

Logback's DBAppender logs all properties in its context and MDC to the database. I would like to control which properties are logged, specifically filtering out certain values, but I can't find any options to do so. The documentation is terse:

The logging_event_property is used to store the keys and values contained in the MDC or the Context

Is it possible to exclude certain properties from being logged?

Here is an example:

Logback is configured with a DBAppender that loads its properties from vct.properties:

<configuration>
    <property resource="vct.properties" />

    <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
            <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <driverClass>com.mysql.jdbc.Driver</driverClass>
                <jdbcUrl>jdbc:mysql://${log.db.host}:${log.db.port}/${log.db.schema}</jdbcUrl>
                <user>${log.db.username}</user>
                <password>${log.db.password}</password>
            </dataSource>
        </connectionSource>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="DB" />
    </root>
</configuration>

vct.properties has the connection settings:

log.db.host=localhost
log.db.port=3306
log.db.schema=logs_development
log.db.username=loguser
log.db.password=logpass

When an event is logged, all of the connection settings are logged:

mysql> select * from logging_event_property where event_id=1;
+----------+---------------------+-------------------------------------------+
| event_id | mapped_key          | mapped_value                              |
+----------+---------------------+-------------------------------------------+
|        1 | log.db.host         | localhost                                 | 
|        1 | log.db.password     | logpass                                   | 
|        1 | log.db.port         | 3306                                      | 
|        1 | log.db.schema       | logs_development                          | 
|        1 | log.db.username     | loguser                                   | 
+----------+---------------------+-------------------------------------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幽蝶幻影 2024-09-04 16:33:27

作为过滤将哪些条目添加到数据库中的替代方法,我编写了一个 Java 程序来将 logback dbappender 数据库列出到控制台,该控制台也支持过滤。

https://github.com/Rockvole/db_logviewer

As an alternative to filtering which entries are added to the database, I wrote a Java program to list the logback dbappender database to the console which also supports filtering.

https://github.com/Rockvole/db_logviewer

ㄖ落Θ余辉 2024-09-04 16:33:26

Logback 目前不支持此功能。如果您还没有输入请求此功能的错误报告,请输入错误报告。

Logback does not currently support this feature. If you haven't already please enter a bug report requesting this feature.

箹锭⒈辈孓 2024-09-04 16:33:26

这些天也遇到了类似的错误。就我而言,我通过定义导入了属性

<springProperty ...> 

。当 springProperty 的范围设置为“context”时,它将自动添加到“logging_event_property”表中。通过删除范围(默认为本地),不再添加 springProperties :-)

Was stumbling over a kinda similar error these days as well. In my case I imported the properties via

<springProperty ...> 

definition. When the scope of the springProperty is set to "context" it will be automatically added to the "logging_event_property" table. By removing the scope (default is local) the springProperties aren't added anymore :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文