Logging a Method Call

The logger provides convenience methods Logger.entering() and Logger.exiting() for logging method calls. This example implements a method that logs the parameters upon entry and the result when returning.

See also Logging an Exception.

package com.mycompany; class MyClass { public boolean myMethod(int p1, Object p2) { // Log entry Logger logger = Logger.getLogger("com.mycompany.MyClass"); if (logger.isLoggable(Level.FINER)) { logger.entering(this.getClass().getName(), "myMethod", new Object[]{new Integer(p1), p2}); } // Method body // Log exit boolean result = true; if (logger.isLoggable(Level.FINER)) { logger.exiting(this.getClass().getName(), "myMethod", new Boolean(result)); // Use the following if the method does not return a value logger.exiting(this.getClass().getName(), "myMethod"); } return result; } }
Here is a sample of the output using a simple formatter and the following call:
new com.mycompany.MyClass().myMethod(123, "hello"); Jan 10, 2002 7:59:48 PM com.mycompany.MyClass myMethod FINER: ENTRY 123 hello Jan 10, 2002 7:59:49 PM com.mycompany.MyClass myMethod FINER: RETURN true Jan 10, 2002 7:59:49 PM com.mycompany.MyClass myMethod FINER: RETURN

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.