Craig wrote:
> - The typical approach with the JSR47 API or with Log4J is to let the
> logger decide whether to really output the message or not. So, you
> would instrument your code with things like (conceptual, not real API):
>
> logger.log("The object is " + myObject, Logger.DEBUG);
>
> and let the logger configuration decide whether you care about debug
> messages or not.
>
> - However, there is a performance problem with leaving this code in --
> you will be doing the string concatenation (and the method call)
> whether or not you want debug messages printed! If you are liberal
> about debug messages, especially inside frequently executed methods,
> this can have adverse impacts on performance (and increased garbage
> collection).
Often the toString() are much worse than the concatenations. After we realized
what a HUGE impact this had on performance, we added a bunch of overloadings
so that you could convert the above to the equivalent of:
logger.log(Logger.DEBUG, "The object is {0}", myObject);
where the formatting is only done once it's known that the category is enabled
(we used named categories instead of levels, but that's another discussion). I just
went and looked at log4j and java.util.logging and it looks like both would require
going through a wrapper class to get this feature.
The number of overloadings is a bit of a pain for the implementer once you
consider that you would often want to log boolean, int, and in some applications
float parameters, but not for the caller.
Louis Tribble