logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: Adding a custom variable to Log4j logging calls
Date Thu, 20 Mar 2008 22:05:30 GMT

On Mar 20, 2008, at 9:59 AM, Tim Watts wrote:

>
> I suppose java.text.MessageFormat would do. For example:
>
> log.debug(MessageFormat.format("Something happened; the context is  
> [{1}]",
> myContextObj));
>
> Although frankly, the simpler approach is something like this:
>
> if (log.isDebugEnabled()) {
>   log.debug("Something happened; the context is [" +myContextObj  
> +"]");
> }
>
> The isDebugEnabled() call is an optimization technique. It allows  
> the program
> to skip the string construction if the log isn't going to write debug
> messages anyway.
>

If you want to use MessageFormat, use LogMF in the extras companion (http://logging.apache.org/log4j/extras

), like:

LogMF.debug(log, "Something happened: the context is [{0}]" ,  
myContextObj);

LogMF is highly optimized to minimize the overhead of using  
MessageFormat, particularly for simple cases this.





---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message