aspose file tools*
The moose likes Performance and the fly likes Using StringBuffer in loggers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Using StringBuffer in loggers" Watch "Using StringBuffer in loggers" New topic
Author

Using StringBuffer in loggers

Srinivasa Kadiyala
Ranch Hand

Joined: Jun 10, 2004
Posts: 237
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


Srini
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8908
    
    8

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:



"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Srinivasa Kadiyala
Ranch Hand

Joined: Jun 10, 2004
Posts: 237
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

Joined: Jun 10, 2004
Posts: 237
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

Joined: Jul 11, 2001
Posts: 14112
Take a look at java.text.MessageFormat.

That's more useful for internationalization, though - simple string concatenation will likely be faster, anyway.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8908
    
    8

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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using StringBuffer in loggers