Tuesday, June 29, 2010

Finally exceptions as parameters in slf4j 1.6

Exceptional parameters

So with slf4j 1.6.0 you can do

try {
something
}
catch (SomeException e) {
log.error("some message {}", parameter, e);
}


That just makes slf4j that much easier to use.

Gotcha
Be aware it is ONLY the last parameter. If the last parameter is an exception the stacktrace will be logged. Looking briefly it appears that the exception remains available for replacement so


log.error("Message {} error {}", "parameter", exception)"

Would look like "Message parameter error exception.getMessage", with a trace after. Don't quote me though, try it for your self.

The old way
It used to be that the e would just be ignored which is very substandard. As you would just never know until you deployed it and it failed and you really wanted the error.

Or you would have to write

try {
something
}
catch (SomeException e) {
log.error("some message " + parameter, e);
}


Logback
If you use logback you should use 0.9.22 to go with slf4j 1.6.0 I found other combinations troublesome.

No comments:

Post a Comment