[EMAIL PROTECTED] wrote:
---- Sebastiaan van Erk <[EMAIL PROTECTED]> wrote:
2) It would be nice to have formatting of log messages as SLF4J has with the MessageFormat api. In combination with varargs (java5); this makes for a really flexible logging mechanism which makes the code much more concise and avoids testing for log level and formatting of messages yourself while still remaining efficient, e.g., something like this:

log.debug("The disk \"{1}\" contains {0} file(s).", count, disk);

Is this really more efficient?

It is certainly a lot more efficient than the naive (without the isDebugEnabled() test):

log.debug("The disk " + disk + " contains " + count + " files");

I would have thought that behind the scenes the compiler turns this into 
exactly the equivalent of:

  Object[] params = new Object[2];
  params[0] = count;
  params[1] = disk;
  log.debug("......", params);


Yes, this is exactly what happens. However this is a relatively cheap operation on modern VM's.
This is not efficient at all; the Object array is created and initialised even 
if debug-level is not enabled and therefore has to be wrapped in
if (log.isDebugEnabled()) anyway.


I would not go so far to say as that it is not efficient at all. I doubt the overhead is really relevant unless you're in a piece of extremely frequently called code, for example an inner loop of a calculation in which the loop body costs very little time to execute relative to the log statement.

Note that even in the case that the overhead is relevant, one is free to add the isDebugEnabled() test. Then there are still advantages:
* the message format approach is more readable
* the message format approach is more flexible (it gives you easy options to format dates, etc.)

Of course you could call MessageFormat.format directly, but this would limit the newly gained readability and convenience, and I also doubt it would see frequent usage.

Regards,
Sebastiaan

I'd be happy to be proved wrong.....


Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to