You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Raman Gupta <ro...@hotmail.com> on 2002/04/27 15:48:39 UTC

Avoid concatentation with string array?

Hello all,

This is a pretty simple idea that I'm sure someone has already thought of 
and rejected, but what if their were versions of the Logger methods debug, 
info, etc. that had a method signature as follows:

   debug(String message, String[] parameters)

The message String would contain "?" a-la prepared statements, to be 
eventually replaced by the values in the string array.

These are the advantages I see:

1) Reducing the need for isDebugEnabled, etc. calls because of the 
elimination of string concatenation until after log4j has checked the 
logging level for the logger.

2) Making the log messages easier to build and understand for programmers 
e.g. instead of building statements like

  "values are '" + var0 + "', '" + var1 + "', "' + var2 + "' and more"

you can just do

  "values are '?', '?', '?' and more", new String[] {var0, var1, var2}

3) Of course, the total cost of logging will be greater because of the 
string parsing and replacement when required, but if using the asynchronous 
appender, the parsing and parameter replacement for the message can be 
deferred and essentially be "free" to the application.

Cheers,
Raman Gupta

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Avoid concatentation with string array?

Posted by Laith Al-Khateeb <lk...@yahoo.com>.
Hi Roman,
I agree with you that not having to perform string
concatenation when the log event level is lower than
the logger level or when the level is disabled would
be better (and without having to use isXXXEnabled(),
but it does not seem that the suggested approach would
mean much less method calls or object creations.

With this approach you would have the string objects
created anyway, and a String array too.

Also, if you have vars of primitive type, int for
exmple, then you would have to call
String.valueOf(int), or Integer.toString(int) before
being able to pass their string representations within
the array.
Which is also done with string concatenation.

Add to that the parsing and replacement required
thatyou mentioned.

So it seems to me that it is just the creation of the
StringBuffer and append() calls on it that would be
eliminated using the suggested methods.

So in your example code, assuming that var0 and var1
are of primitive types:

if (logger.isDebugEnabled()) {
logger.debug("values are '" + var0 + "', '" + var1 +
"', "' + var2 + "' and more");
}

you would need
String var0 = String.valueOf(var0Int);
String var1 = String.valueOf(var1Int);
logger.debug("values are '?', '?', '?' and more", new
String[] {var0, var1, var2});

Regards,
Laith

--- Raman Gupta <ro...@hotmail.com> wrote:
> Hello all,
> 
> This is a pretty simple idea that I'm sure someone
> has already thought of 
> and rejected, but what if their were versions of the
> Logger methods debug, 
> info, etc. that had a method signature as follows:
> 
>    debug(String message, String[] parameters)
> 
> The message String would contain "?" a-la prepared
> statements, to be 
> eventually replaced by the values in the string
> array.
> 
> These are the advantages I see:
> 
> 1) Reducing the need for isDebugEnabled, etc. calls
> because of the 
> elimination of string concatenation until after
> log4j has checked the 
> logging level for the logger.
> 
> 2) Making the log messages easier to build and
> understand for programmers 
> e.g. instead of building statements like
> 
>   "values are '" + var0 + "', '" + var1 + "', "' +
> var2 + "' and more"
> 
> you can just do
> 
>   "values are '?', '?', '?' and more", new String[]
> {var0, var1, var2}
> 
> 3) Of course, the total cost of logging will be
> greater because of the 
> string parsing and replacement when required, but if
> using the asynchronous 
> appender, the parsing and parameter replacement for
> the message can be 
> deferred and essentially be "free" to the
> application.
> 
> Cheers,
> Raman Gupta
> 
>
_________________________________________________________________
> MSN Photos is the easiest way to share and print
> your photos: 
> http://photos.msn.com/support/worldwide.aspx
> 
> 
> --
> To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 


__________________________________________________
Do You Yahoo!?
Yahoo! Health - your guide to health and wellness
http://health.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>