e387. logging a method call.txt
来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 40 行
TXT
40 行
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 + =
减小字号Ctrl + -
显示快捷键?