如何 用slf4j logback 把日志异步记录到数据库中

如何 用slf4j logback 把日志异步记录到数据库中,第1张

<!-- 将日志存储到oracle数据库中 -->

<appender name="db-classic-oracle" class="ch.qos.logback.classic.db.DBAppender">

<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">

<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">

<driverClass>oracle.jdbc.driver.OracleDriver</driverClass>

<url>jdbc:oracle:thin:@localhost:1521:ZYD</url>

<user>scott</user>

<password>tiger</password>

<sqlDialect class="ch.qos.logback.core.db.dialect.OracleDialect" />

</dataSource>

</connectionSource>

<sqlDialect class="ch.qos.logback.core.db.dialect.OracleDialect" />

</appender>

首先是下载所需要的包,然后在\logback-1.1.2\logback-classic\src\main\java\ch\qos\logback\classic\db\script下你能看到创建日志所需要的的建表脚本

然后就是配置了。

首先是报错的配置,也是网上找资料别人给的配置……

复制代码

<!-- 将日志存储到oracle数据库中 -->

<appender name="db-classic-oracle" class="ch.qos.logback.classic.db.DBAppender">

<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">

<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">

<driverClassName>oracle.jdbc.driver.OracleDriver</driverClassName>

<url>jdbc:oracle:thin:@localhost:1521:ZYD</url>

<user>scott</user>

<password>tiger</password>

</dataSource>

</connectionSource>

</appender>

复制代码

运行一下,你会发现会报这个错误:DBAppender cannot function if the JDBC driver does not support getGeneratedKeys method *and* without a specific SQL dialect

然后把气冲冲的加上了方言……

复制代码

<!-- 将日志存储到oracle数据库中 -->

<appender name="db-classic-oracle" class="ch.qos.logback.classic.db.DBAppender">

<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">

<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">

<driverClassName>oracle.jdbc.driver.OracleDriver</driverClassName>

<url>jdbc:oracle:thin:@localhost:1521:ZYD</url>

<user>scott</user>

<password>tiger</password>

<sqlDialect class="ch.qos.logback.core.db.dialect.OracleDialect" />

</dataSource>

</connectionSource>

</appender>

复制代码

结果还是不对!

最后跑去Stack OwerFlow网站看了一下那帮外国程序员是怎么解决的,终于在看了几个帮助之后

发现了……

<driverClassName>oracle.jdbc.driver.OracleDriver</driverClassName>

原来还有它的错,正确的应该是

复制代码

<!-- 将日志存储到oracle数据库中 -->

<appender name="db-classic-oracle" class="ch.qos.logback.classic.db.DBAppender">

<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">

<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">

<driverClass>oracle.jdbc.driver.OracleDriver</driverClass>

<url>jdbc:oracle:thin:@localhost:1521:ZYD</url>

<user>scott</user>

<password>tiger</password>

<sqlDialect class="ch.qos.logback.core.db.dialect.OracleDialect" />

</dataSource>

</connectionSource>

</appender>

复制代码

再试一下,终于成功了!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6658050.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存