On Jul 15, 2012, at 6:36 AM, Gary Gregory wrote:

> Hi All:
> 
> I've only now been able to take a peek at v2 in SVN so... sorry for the late 
> email but I feel better late than never.
> 
> With log4j 1.2, my usage pattern is:
> 
> logger.debug(String.format("This %s and that %d on this day %1$tm 
> %1$te,%1$tY", this, this.getThat(), this.getCalendar()));
> 
> In 2.0, we have this example:
> 
> logger.debug("Logging in user {} with id {}", user.getName(), user.getId());
> 
> Why invent a new syntax when String.format is baked into the JRE? 

Because you are doing the string formatting whether the log event will be 
logged or not, which means your logger.debug should always be wrapped in an if 
isDebugEnabled.  Also, the current syntax is compatible with SLF4J. See 
http://www.slf4j.org/faq.html#logging_performance.

If you want to write your debug message the way you have shown there is nothing 
that says you can't. It will still work.  However, as I recall Ceki ran tests 
that showed the Java text formatting is pretty slow.  See 
http://bugzilla.slf4j.org/show_bug.cgi?id=116 for a nice discussion on this, 
and related topics.

One other point.  The String with placeholders is handled by 
ParamaterizedMessage.  This class could either be extended or enhanced to 
support another syntax. If it is extended then that class would have to be used 
in the logging call such as

        logger.debug(new FormattedMessage("This %s and that %d on this day 
%1$tm %1$te,%1$tY", this, this.getThat(), this.getCalendar()));

The advantage with the above is that, although a FormattedMessage is created 
every time the logger.debug statement is executed, it will defer the actual 
message formatting until the event is logged (i.e. the formatting won't happen 
if debug logging is disabled).

Ralph


Reply via email to