![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e388. Logging an ExceptionThe logger methodlog() 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)
e386. Determining If a Message Will Be Logged e387. Logging a Method Call e389. Minimizing the Impact of Logging Code e390. Preventing a Logger from Forwarding Log Records to Its Parent e391. Writing Log Records to a Log File e392. Writing Log Records to Standard Error e393. Writing Log Records Only After a Condition Occurs e394. Setting a Filter on a Logger Handler
© 2002 Addison-Wesley. |