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 Chris <sh...@yahoo.com> on 2007/01/24 07:17:48 UTC

Unnecessary garbage

Why does the logger force you to use Strings?

logger.info("some string here" + someValue);

In most cases, you're going to be creating some dynamic value that must 
be constructed, which means that you are always better off using a char 
array or a StringBuffer to build it. These buffers can be reused. A 
String, on the other hand, always creates unnecessary garbage.

Am I crazy, or was this just a poor design decision?

It seems pretty clear that there ought to be a method like this:

// initialized once
StringBuffer buf = ...

// to log a new message
buf.clear();
buf.append("some string here");
buf.append(someValue);
logger.info(buf);


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


Re: Unnecessary garbage

Posted by Curt Arnold <ca...@apache.org>.
On Jan 24, 2007, at 12:17 AM, Chris wrote:

> Why does the logger force you to use Strings?
>
> logger.info("some string here" + someValue);
>
> In most cases, you're going to be creating some dynamic value that  
> must be constructed, which means that you are always better off  
> using a char array or a StringBuffer to build it. These buffers can  
> be reused. A String, on the other hand, always creates unnecessary  
> garbage.
>
> Am I crazy, or was this just a poor design decision?
>
> It seems pretty clear that there ought to be a method like this:
>
> // initialized once
> StringBuffer buf = ...
>
> // to log a new message
> buf.clear();
> buf.append("some string here");
> buf.append(someValue);
> logger.info(buf);


Actually that should work, all the logging methods take an Object and  
call the toString() method to get a string when resolving the final  
message.

However if you are doing a lot of that type of stuff, I would suggest  
looking at the formatter project in the log4j sandbox, where you  
would do something like:

LogMF.info(logger, "some string here{0}", someValue);

log4j 1.3 has its own unusual formatting specification which would be  
something like:

logger.info("some string here{}", someValue);





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