Logging an Exception

The logger method log() can be used to log an exception. Also, the convenience method Logger.throwing() can be used by a method about to throw an exception.
package com.mycompany; class MyClass { public void myMethod() { Logger logger = Logger.getLogger("com.mycompany.MyClass"); // This method should be used when an exception is encounted try { // Test with an exception throw new IOException(); } catch (Throwable e){ // Log the exception logger.log(Level.SEVERE, "Uncaught exception", e); } // When a method is throwing an exception, this method should be used Exception ex = new IllegalStateException(); logger.throwing(this.getClass().getName(), "myMethod", ex); } }
Here is a sample of the output generated by the example:
Jan 11, 2002 5:16:49 PM com.mycompany.MyClass myMethod SEVERE: Uncaught exception java.io.IOException at com.mycompany.MyClass.myMethod(com.mycompany.MyClass.java:32) at com.mycompany.MyClass.main(com.mycompany.MyClass.java:18) Jan 11, 2002 5:16:50 PM com.mycompany.MyClass myMethod FINER: THROW java.lang.IllegalStateException at com.mycompany.MyClass.myMethod(com.mycompany.MyClass.java:25) at com.mycompany.MyClass.main(com.mycompany.MyClass.java:18)

Comments

26 Jan 2010 - 8:49am by Anonymous (not verified)

thxxxxxxx

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.