If your log messagge would not be logged with the current settings and it contains relative expensive operation like a string concatenation, it's better to precede it with a less expensive operation like a check:
if (logger.isLoggable(Level.FINEST)) {
logger.finest("log this: "+value);
}To log an exception and its stacktrace use:
try {
// Test with an exception
throw new Exception();
} catch (Throwable e) {
// Log the exception
logger.log(Level.SEVERE, "your message", e);
}You can also you when you enter in a method and with which parameters, if you throws exception in the method and when you exit and with with return value.
This is an example:
public int myMethod(String p1, int p2) {
log.entering(MyClass.class.getName(), "myMethod",
new Object[] { p1, p2 });
if (p2 < 0) {
Exception ex = new IllegalStateException();
log.throwing(this.getClass().getName(), "myMethod", ex);
}
int result = p2 * 2;
log.exiting(MyClass.class.getName(), "myMethod", result);
return result;
}
Note that this approach is very intrusive and inquinates you code logic, therefore it would be better to do this using an interceptor.
No comments:
Post a Comment