• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using StringBuffer in loggers

 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find many people are using string concatination for logging.

for example:
String path="http://www.xyz.com";
String tempVar="testing";
logger.info("Successfully completed task: " + tempVar + ". Path = " + path);

Instead of the above code..I would like to write :

logger.info(new StringBuffer().append("Successfully completed task: ").append(tempVar).append(". Path = ").append(path).toString());


Thanks in advance to all
 
Joe Ess
Bartender
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Puthriah Sarma:

logger.info(new StringBuffer().append("Successfully completed task: ").append(tempVar).append(". Path = ").append(path).toString());


That's actually what the compiler will generate from your previous statement. It uses StringBuffer to implement concatenation. As long as you have the concatenation in the same statement, you would not gain anything by creating your own StringBuffer. Now if you had the concatenation spanning several statements:

it would create a number of StringBuffers, one for each concatenation. This may or may not be a big deal depending on your situation. Now, why build that String in the first place if you are never going to use it, like if the debug level is above INFO? The Log4J manual recommends checking if the String is needed before constructing it:

Log4J also has methods which let you use message parameters, so the string doesn't get created unless it is used:

 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joe.

Regret for my duplicate entry.

I wrote there also as I find very few entries here and I got a doubt that this is an unattended or less priority area.
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe

I have a problem.

I am using java.util.logging and not Log4J. The conventions ({0} and {1}suggested by you are not working with info().

Can you advise ?

Thanks in advance
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at java.text.MessageFormat.

That's more useful for internationalization, though - simple string concatenation will likely be faster, anyway.
 
Joe Ess
Bartender
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Puthriah Sarma:

Regret for my duplicate entry.

No worries. I'm just trying to keep the community from duplicating effort.
Originally posted by Puthriah Sarma:

I am using java.util.logging and not Log4J. The conventions ({0} and {1}suggested by you are not working with info().

java.util.logging does not have that feature. You are pretty much limited to something like:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, java.util.logging does have equivalent methods. The java.util.logging version is:

They haven't provided corresponding overloads for info() etc, so you have to specify Level directly with the first parameter. And now that JDK 5 is out, you'd think they would have replaced that final array with a vararg so we don't have to say "new Object[]" - but they haven't, yet. Feel free to vote for this bug if you'd like to see this trivial change made to enhance usability of java.util.logging. Actually as I think about it, it would be even better if Logger had methods which use the new Formatter functionality, as that's much more powerful and already integrated into widely-used classes like String and PrintStream (with methods like printf()).

By the way, the preferred way to say

is probably

[ July 30, 2005: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic