Pawan
Pawan

Reputation: 32321

Log Exception Stack Trace with Log4j JDBCAppender

We are using log4j JDBCAppender in our project. But, its not able to log exception stack traces to Database. I searched through the net, and i got %throwable, but this is not working with JDBCAppender. Can someone help me out in doing this?

This is my custom Appender code which i used as same of JDBCAppender with small modifications

public void append(LoggingEvent event) {
        ArrayList buffer = new ArrayList();

        buffer.add(event);

        flushBuffer(buffer);
    }



    public void flushBuffer(ArrayList buffer) {

        for (Iterator i = buffer.iterator(); i.hasNext();) {
            try {
                LoggingEvent logEvent = (LoggingEvent) i.next();
                String messageRecievied = getLogStatement(logEvent);
                execute(messageRecievied);

            } catch (SQLException e) {
                this.errorHandler.error("Failed to excute sql", e, 2);
            }

        }

    }

I dont know why its not including the stacktraces

This is my log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 <appender class="com.MyAppender" name="customAppen">
      <layout class="org.apache.log4j.EnhancedPatternLayout">
            <param value="%d[%t] %-5p(%F:&lt;%M&gt;:%L)- %m%n" name="ConversionPattern"/>
        </layout>
    </appender>
 <appender class="org.apache.log4j.AsyncAppender" name="asynchapp">
  <param name="Blocking" value="false"/> 
    <appender-ref ref="customAppen"/>
    </appender>
    <root>
<level value = "INFO"/>
    <appender-ref ref="asynchapp"/>
</root>

Upvotes: 0

Views: 598

Answers (1)

Guillaume Darmont
Guillaume Darmont

Reputation: 5018

The JDBCAppender javadoc says explicitly it does not log exceptions. You have to write a custom appender yourself. But I'm pretty sure that such thing already exists.

Upvotes: 1

Related Questions