mix logger.error(String format, Object... arguments) and logger.error(String msg, Throwable t) #296
Replies: 4 comments 9 replies
-
@ManfredSchenkIOSB Are you aware of this faq entry? Note that starting with 2.0.0, you can also write
|
Beta Was this translation helpful? Give feedback.
-
Unless I am misunderstanding you, this is actually already possible. See here. Basically when the last argument is a String s = "Hello world";
try {
Integer i = Integer.valueOf(s);
} catch (NumberFormatException e) {
logger.error("Failed to format {}", s, e);
} It would be nice if this were clearly mentioned in the Javadoc on the methods though. I forget it often myself. (In fact that's why I stumbled here, wondering if it could be added.) It is (in my opinion, cryptically) mentioned here in the Javadoc. |
Beta Was this translation helpful? Give feedback.
-
Sorry but that is a bad idea for a strongly-typed language like Java. Giving special treatment for an argument simply because of its position in a method's signature is a convention-based approach that should have been replaced by one that is based on the argument's type. Why not simply put the "Throwable" type as the very first argument in the Logger.log(Throwable t, String message, Object... args) signature? It seems to be straight-forward API design 101. What am I missing? Yes, the fluent API in version 2.x is nice but that doesn't solve the earlier bad API design as-is. |
Beta Was this translation helpful? Give feedback.
-
This has been the source of much confusion and some mistakes in many companies I've worked in. But even if it has to be there, it should be stated on the method-level Javadoc as well. |
Beta Was this translation helpful? Give feedback.
-
from time to time I am missing some kind of combination of the methods mentioned in the discussion title. Within a catch clause I'm trying to log some parameters as well as the Exception. At the moment, I construct the msg text by myself, but a mix of the methods would be nice. The same is true for all methods taking a Throwable as parameter.
But I see there are points in favor of such a method and also against this method:
positive:
negative:
Any opinions on this?
Beta Was this translation helpful? Give feedback.
All reactions