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>
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:
The DBAppender inserts logging events into three database tables in a format independent of the Java programming language.
These three tables are logging_event, logging_event_property and logging_event_exception. They must exist before DBAppender can be used.
To my understanding, this means that I'm bound to those 3 tables to log to the database.
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?
Your help is greatly appreciated.
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).
If you wish to customize table and/or column names, you can implement DBNameResolver
.