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.
Here is a sample of the output using a simple formatter and the
following call:
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;
}
}
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