带有登录的异步DBAppender [英] Asynchronous DBAppender with logback
问题描述
我正在开发一个使用logback进行日志记录的应用程序.我使用了logback DBAppender将日志插入数据库.
I'm working on an application that uses logback for logging. I used the logback DBAppender to insert logs into DB.
对我来说,一切都很好,我能够将日志插入数据库中并查看日志.
All worked great for me and i was able to insert and see the logs into the Database.
我用200条记录的代码行进行了一些测试,并测量了这200条日志的时间消耗,并且在不使用DBAppender的情况下获得了10毫秒的时间.当我添加DBAppender时,我大约需要2700毫秒.
I did some testing with 200 logging line of code and measured the time consumption for those 200 logs and got 10 milliseconds without using the DBAppender. When i added the DBAppender i got around 2700 milliseconds.
我尝试在没有运气的情况下使用ch.qos.logback.classic.AsyncAppender
使其异步工作.我总是将0个结果插入到我的数据库中.
I tried to use the ch.qos.logback.classic.AsyncAppender
with no luck making it work asynchronously. I always get 0 results inserted into my database.
我的配置如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are by default assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="DBAppPostgreSQL" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
<driverClass>org.postgresql.Driver</driverClass>
<url>jdbc:postgresql://127.0.0.1:5678/DB</url>
<user>YYYYY</user>
<password>XXX</password>
</dataSource>
</connectionSource>
<sqlDialect class="ch.qos.logback.core.db.dialect.PostgreSQLDialect" />
<insertHeaders>true</insertHeaders>
</appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="DBAppPostgreSQL" />
<includeCallerData>true</includeCallerData>
</appender>
<root level="debug">
<appender-ref ref="ASYNC" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
我的配置是否缺少任何内容?还是有其他方法可以使其更快地将日志插入数据库?
Is there anything missing with my configuration? Or is there any other way to make it insert logs into DB faster?
请注意,我正在使用postgreSQL
Note that i'm using postgreSQL
谢谢
已
我在slf4j 1.7.5上使用了logback 1.0.11.另外我正在使用PostgreSQL 8.0.325.
I'm using logback 1.0.11 over slf4j 1.7.5. Also i'm using postgresql 8.0.325.
登录的内部状态为:
10:52:09,693 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
10:52:09,693 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
10:52:09,693 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/BOOM/BoomFinancial/BoomMonitoringTesting/bin/logback.xml]
10:52:09,759 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
10:52:09,761 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
10:52:09,771 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
10:52:09,800 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.db.DBAppender]
10:52:09,803 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [DBAppPostgreSQL]
10:52:09,809 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@14:75 - no applicable action for [dataSource], current pattern is [[configuration][appender][connectionSource][dataSource]]
10:52:09,866 |-INFO in ch.qos.logback.core.db.dialect.DBUtil@30e34726 - Could not call supportsGetGeneratedKeys method. This may be recoverable
10:52:09,866 |-INFO in ch.qos.logback.core.db.DriverManagerConnectionSource@195428dd - Driver name=PostgreSQL Native Driver
10:52:09,866 |-INFO in ch.qos.logback.core.db.DriverManagerConnectionSource@195428dd - Driver version=PostgreSQL 8.0 JDBC2 with NO SSL (build 325)
10:52:09,866 |-INFO in ch.qos.logback.core.db.DriverManagerConnectionSource@195428dd - supportsGetGeneratedKeys=false
10:52:09,867 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@21:77 - no applicable action for [sqlDialect], current pattern is [[configuration][appender][sqlDialect]]
10:52:09,868 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@22:21 - no applicable action for [insertHeaders], current pattern is [[configuration][appender][insertHeaders]]
10:52:09,870 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.AsyncAppender]
10:52:09,872 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [ASYNC]
10:52:09,872 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [DBAppPostgreSQL] to ch.qos.logback.classic.AsyncAppender[ASYNC]
10:52:09,872 |-INFO in ch.qos.logback.classic.AsyncAppender[ASYNC] - Attaching appender named [DBAppPostgreSQL] to AsyncAppender.
10:52:09,874 |-INFO in ch.qos.logback.classic.AsyncAppender[ASYNC] - Setting discardingThreshold to 51
10:52:09,874 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
10:52:09,875 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [ASYNC] to Logger[ROOT]
10:52:09,875 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
10:52:09,875 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
10:52:09,876 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@23e0512a - Registering current configuration as safe fallback point
推荐答案
logback报告了一条错误消息:
There is one error message reported by logback:
0:52:09,809 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@14:75 - no applicable action for [dataSource], current pattern is [[configuration][appender][connectionSource][dataSource]]
这表明无法理解嵌套在其中的元素.这是配置文件的相关部分:
This indicates that the element nested within could not be understood. Here is the relevant part of the configuration file:
<appender name="DBAppPostgreSQL" class="ch.qos.logback.classic.db.DBAppender">
<!-- DriverManagerConnectionSource does not have a dataSource property -->
<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
<driverClass>org.postgresql.Driver</driverClass>
<url>jdbc:postgresql://127.0.0.1:5678/DB</url>
<user>YYYYY</user>
<password>XXX</password>
</dataSource>
</connectionSource>
<sqlDialect class="ch.qos.logback.core.db.dialect.PostgreSQLDialect" />
<insertHeaders>true</insertHeaders>
</appender>
ch.qos.logback.core.db.DriverManagerConnectionSource
没有dataSource
属性,而ch.qos.logback.core.db.DataSourceConnectionSource
有.您可能要使用以下命令配置DBAppPostgreSQL:
ch.qos.logback.core.db.DriverManagerConnectionSource
does not have a dataSource
property whereas ch.qos.logback.core.db.DataSourceConnectionSource
does. You probably want to configure DBAppPostgreSQL with:
<appender name="DBAppPostgreSQL" class="ch.qos.logback.classic.db.DBAppender">
<!-- use DataSourceConnectionSource instead of DriverManagerConnectionSource -->
<connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
<driverClass>org.postgresql.Driver</driverClass>
<url>jdbc:postgresql://127.0.0.1:5678/DB</url>
<user>YYYYY</user>
<password>XXX</password>
</dataSource>
</connectionSource>
<sqlDialect class="ch.qos.logback.core.db.dialect.PostgreSQLDialect" />
<insertHeaders>true</insertHeaders>
</appender>
这篇关于带有登录的异步DBAppender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!