User Tools

Site Tools


java:writingerrorsinlog

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
java:writingerrorsinlog [2012/05/16 16:51] – creado rlunarojava:writingerrorsinlog [2022/12/02 22:02] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== 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.  
 +
 +<code java>
 +
 +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()
 +
 +</code>
java/writingerrorsinlog.txt · Last modified: 2022/12/02 22:02 by 127.0.0.1