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]
rlunaro creado
java:writingerrorsinlog [2014/12/24 10:42] (current)
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: 2014/12/24 10:42 (external edit)