使用LogBack登录数据库 [英] Log to database with LogBack

查看:161
本文介绍了使用LogBack登录数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用log4j,我曾经有一个直接记录到我的数据库中的一个表的appender。 appender如下:

Using log4j, I used to have an appender that logs directly to one of the tables in my database. The appender was as follows:

 <appender name="DATABASE_LOG" class="org.apache.log4j.jdbc.JDBCAppender">
        <param name="URL" value="jdbc:postgresql://localhost/registrationdb" />
        <param name="Driver" value="org.postgresql.Driver"/>
        <param name="User" value="postgres"/>
        <param name="Password" value="********/>
        <layout class="org.apache.log4j.EnhancedPatternLayout">
            <param name="ConversionPattern" value="INSERT INTO user (user_id,creation_datetime,comment,user_type) VALUES ('%X{userId}','%d{yyyy-MM-dd HH:mm:ss}','%X{comment}','%X{userType}')"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG" />
            <param name="LevelMax" value="INFO" />
        </filter>
  </appender>

log4j是部署在JBoss上的Web应用程序的一部分4.2.log4j的问题在于它没有关闭它打开的数据库连接,甚至当我们在Web服务器上尝试Postgres数据源时,它正在耗尽池中的所有连接(这是一个完全不同的问题)这就是我们的原因决定尝试LogBack,看看它是否更好。有趣的是,当我阅读LogBack文档时,它清楚地提到了以下内容:

The log4j was part of a web application that's deployed on JBoss 4.2. The problem with log4j was that it wasn't closing the database connection it's opening, and even when we tried a Postgres data source on the web server, it was depleting all the connections in the pool(This is a whole different issue). This is why we decided to try LogBack, to see if it's any better. Interestingly, when I read the LogBack documentation, it clearly mentioned the following:


DBAppender将记录事件插入到$ b中的三个数据库表中$ b格式独立于Java编程语言。

The DBAppender inserts logging events into three database tables in a format independent of the Java programming language.

这三个表是logging_event,logging_event_property和
logging_event_exception。它们必须在DBAppender使用
之前存在。

These three tables are logging_event, logging_event_property and logging_event_exception. They must exist before DBAppender can be used.

根据我的理解,这意味着我必须遵守那些3用于登录数据库的表。

To my understanding, this means that I'm bound to those 3 tables to log to the database.

在互联网上搜索解决方案产生的结果非常少,涉及扩展 AppenderBase 在这个阶段对我来说不方便的课程,因为这意味着必须进行更多的测试,因为我们知道我们有很多其他的测试要求。问题是:我可以为LogBack定义一个与我上面的log4j appender功能类似的appender吗?

Searching for solutions on the internet has yielded very few results that involved extending the AppenderBase class which is not convenient for me at this stage, since this means that more testing will have to be done, knowing that we have plenty of other requirements to test. The question is: Can I define an appender for LogBack that's similar in functionality to my log4j appender above?

非常感谢你的帮助。

推荐答案

是的,Logback需要3个数据库表,你不能跳过它们中的任何一个,你只能自定义它们的名字。这实际上是一个优点,Log4J根本没有记录异常,而Logback使用单独的表来进行堆栈跟踪。它还会记录 mdc 属性(第三张表)。

Yes, Logback requires 3 database tables, you cannot skip any of them, you can only customize their names. This is actually an advantage, Log4J didn't log exceptions at all, while Logback uses a separate table for stack trace lines. It also logs mdc properties (third table).

如果您想自定义表名和/或列名,可以实现 DBNameResolver

If you wish to customize table and/or column names, you can implement DBNameResolver.

这篇关于使用LogBack登录数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆