You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by James Stauffer <js...@spscommerce.com> on 2004/05/14 01:51:12 UTC

DBAppender logging_event_exception

I wrote my own DBAppender for use with 1.2.8 that also uses our own
connection pool.  In our code we often catch an exception, covert the stack
trace to a string, add some info, and throw a new exception.  That causes
exception messages to be very long.  One thing that I did in my DBAppender
was take each line from event.getThrowableStrRep() and split it into lines.
I know that isn't needed a lot now because many Exceptions have a cause but
many still don't and people may have a lot of code that still does like we
do.  Anyway I thought I would bring this up in case you wanted to do a
similar thing.  Code below.  Note that I also put a try/catch around each
insert so that if one line is too long the others will still be inserted but
I am not using transactions so you many not be able to do that in one
transaction.

            String[] strRep = event.getThrowableStrRep();
            if(strRep != null) {
//                LogLog.debug("Exception Strings: " + strRep.length);
                List lines = new ArrayList(strRep.length);
                for (short i = 0; i < strRep.length; i++) {
                    StringTokenizer st = new StringTokenizer(strRep[i],
"\r\n");
                    while(st.hasMoreTokens()) {
                        String line = st.nextToken();
                        if(line.length() > 254) {
                            line = line.substring(0, 254);
                        }
                        lines.add(line);
                    }
                }
//                LogLog.debug("Exception Lines: " + lines.size());
                
                for (short i = 0; i < lines.size(); i++) {
                    try {
                        sql = EVENT_EXCEPTION_INSERT 
                            + DatabaseManager.Value(eventId) + ", "
                            + DatabaseManager.Value(i) + ", "
                            + DatabaseManager.Value((String)lines.get(i)) +
")";
//                        LogLog.debug("SQL: " + sql);
                        DatabaseManager.ExecuteInsert(sql);
                    } catch(SQLException e) {
                        LogLog.error("Problem writing Exception lines", e);
                    }
                }

James Stauffer