java:writingerrorsinlog
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
java:writingerrorsinlog [2012/05/16 14:51] – creado rlunaro | java:writingerrorsinlog [2022/12/02 21: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' | ||
+ | |||
+ | <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: | ||
+ | { | ||
+ | log.error( "at " + stackElem.getClassName() | ||
+ | + " | ||
+ | + stackElem.getMethodName() | ||
+ | + "( " | ||
+ | + stackElem.getFileName() | ||
+ | + ":" | ||
+ | + stackElem.getLineNumber() +" | ||
+ | } | ||
+ | | ||
+ | } // log.isErrorEnabled() | ||
+ | | ||
+ | | ||
+ | } // logStackTrace() | ||
+ | |||
+ | </ |