📄 e387. logging a method call.txt
字号:
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 e388 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
Related Examples
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -