User Tools

Site Tools


java:writingerrorsinlog

Writing properly errors in log file

I've search ways to properly write one error into Apache Commons Log and I've haven't found anything. At last I've decided to create my own method. Here it is (see below). I've implemented it as an static method, but you can configure in your own way.

import org.apache.commons.logging.Log;
 
    public static void logStackTrace( Log log, Exception e )
    {
        // get the stack trace
        StackTraceElement[] stack = e.getStackTrace();
 
        if( log.isErrorEnabled() )
        {
            log.error( e.getMessage() );
            for( StackTraceElement stackElem:stack )
            {
                log.error( "at " + stackElem.getClassName() 
                        + "."
                        + stackElem.getMethodName() 
                        + "( " 
                        + stackElem.getFileName() 
                        + ":" 
                        + stackElem.getLineNumber() +")"  );
            }
 
        } // log.isErrorEnabled()
 
 
    } // logStackTrace()
java/writingerrorsinlog.txt · Last modified: 2014/12/24 10:42 (external edit)