<div>I wrote a custom database appender and I just extended AppenderBase<ILoggingEvent>.  In the start() method I get the DataSource from a JNDI lookup.  Since I'm using Spring JDBC under the hood I use the DataSource to instantiate a JDBCTemplate which is used in the append() method.</div>

<div> </div>
<div>Paul</div>
<div><br><br> </div>
<div class="gmail_quote">On Sun, Oct 16, 2011 at 7:51 PM, Stevo Slavić <span dir="ltr"><<a href="mailto:sslavic@gmail.com">sslavic@gmail.com</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">Hello Logback users,<br><br>As subject states, I'm trying to migrate a project from log4j to<br>logback, and need to replace log4j JDBCAppender.<br>
<br>I've found following related stackoverflow questions:<br><br>- <a href="http://stackoverflow.com/questions/1364322/log-to-a-database-using-log4j/1366684#1366684" target="_blank">http://stackoverflow.com/questions/1364322/log-to-a-database-using-log4j/1366684#1366684</a><br>
- <a href="http://stackoverflow.com/questions/7659837/logback-dbappender-custom-sql" target="_blank">http://stackoverflow.com/questions/7659837/logback-dbappender-custom-sql</a><br><br>DBAppender is nice, but it's not 1-to-1 replacement for log4j's<br>
JDBCAppender, since it mandates what gets logged, db layout (number of<br>tables and columns) and thus SQL statements number and layout (number<br>of and indexes of parameters).<br><br>Trying now to implement custom extension of DBAppenderBase to achieve the goal.<br>
<br>It would be great if in the logback DB appender hierarchy was<br>additional level, before DBAppenderBase which would abstract away just<br>ConnectionSource related logic, so without subAppend,<br>secondarySubAppend, or selectEventId.<br>
<br>Also, to support using to-be-append logging event when generating<br>insert SQL (e.g. using PatternLayout) it would be good if getInsertSQL<br>would receive ILoggingEvent.<br><br>Now when implementing JDBCAppender which extends DBAppenderBase I have<br>
lots of abstract methods "do-nothing" implementations, pattern layout<br>configured with insert statement SQL and following relevant code:<br><br><br>       protected Layout<ILoggingEvent> layout;<br><br>       protected void setLayout(Layout<ILoggingEvent> layout) {<br>
               this.layout = layout;<br>       }<br><br>       protected String getInsertSQL(ILoggingEvent event) {<br>               return layout.doLayout(event);<br>       }<br><br>       @Override<br>       public void append(ILoggingEvent eventObject) {<br>
               Connection connection = null;<br>               try {<br>                       connection = connectionSource.getConnection();<br>                       connection.setAutoCommit(false);<br>                       PreparedStatement insertStatement;<br>
<br>                       insertStatement = connection<br>                                       .prepareStatement(getInsertSQL(eventObject));<br><br>                       int updateCount = insertStatement.executeUpdate();<br>
                       if (updateCount != 1) {<br>                             addWarn("Failed to insert loggingEvent");<br>                       }<br><br>                       // we no longer need the insertStatement<br>
                       close(insertStatement);<br>                       insertStatement = null;<br><br>                       connection.commit();<br>               } catch (Throwable sqle) {<br>                       addError("problem appending event", sqle);<br>
               } finally {<br>                       DBHelper.closeConnection(connection);<br>               }<br>       }<br><br><br><br>What do you think about improvement requests? Should I log them in JIRA?<br><br>Any thoughts on this JDBCAppender replacement? What will be the<br>
penalty for generating insert statement SQL for every logging event<br>using Layout?<br><br>Regards,<br>Stevo.<br>_______________________________________________<br>Logback-user mailing list<br><a href="mailto:Logback-user@qos.ch">Logback-user@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/logback-user" target="_blank">http://mailman.qos.ch/mailman/listinfo/logback-user</a><br></blockquote></div><br>