2

我正在开发一个使用 logback 进行日志记录的应用程序。我使用 logback DBAppender 将日志插入 DB。

一切对我来说都很好,我能够将日志插入并查看到数据库中。

我使用 200 行日志记录代码进行了一些测试,并测量了这 200 条日志的时间消耗,并在不使用 DBAppender 的情况下获得了 10 毫秒。当我添加 DBAppender 时,我得到了大约 2700 毫秒。

我尝试使用它ch.qos.logback.classic.AsyncAppender,但没有让它异步工作。我总是将 0 个结果插入到我的数据库中。

我的配置如下所示:

<?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>

我的配置有什么遗漏吗?或者有没有其他方法可以让它更快地将日志插入数据库?

请注意,我正在使用 postgreSQL

谢谢,

编辑:

我在 slf4j 1.7.5 上使用 logback 1.0.11。我也在使用 postgresql 8.0.325。

logback 的内部状态是:

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
4

2 回答 2

4

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]]

这表明嵌套在其中的元素无法被理解。这是配置文件的相关部分:

<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:

<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>
于 2013-04-11T09:28:32.787 回答
0

感谢Ceki的回答。我无法评论它,所以我添加了一个新答案:Ceki 的答案包含另一件事是不正确的:元素“ url ”应替换为“ jdbcUrl ”。所以配置应该是这样的:

<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>
      <jdbcUrl>jdbc:postgresql://127.0.0.1:5678/DB</jdbcUrl>
      <user>YYYYY</user>
      <password>XXX</password>
    </dataSource>
  </connectionSource>
  <sqlDialect class="ch.qos.logback.core.db.dialect.PostgreSQLDialect" />
  <insertHeaders>true</insertHeaders>
</appender>
于 2013-10-01T19:08:24.270 回答