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 Ray Kiddy <ra...@ganymede.org> on 2013/06/09 23:36:22 UTC

better way to handle objects in messages?

I have done something and can see where it might be generalized to help other users of log4j. Has this issue already been discussed or dealt with in some other way?

I found a bunch of these in a large legacy application:

	log.debug("the object "+obj+" is something something blah blah blah");

The problem, of course, is that regardless of whether the Level will lead to the statement being logged out, the obj's toString() method gets called. This can lead, at times, to quite a bit of work. And that works gets thrown away almost all of the time. So I have written a class that I send all of my log statements through. That class has methods:

	static void info(Logger log, Object… objs);
	static void debug(Logger log, Object… objs);
	static void error(Logger log, Object… objs);

In the info() method, the Level is checked. If it is going to print, then the obis are iterated through. And for some kinds of objects we call toString but some objects are handled in a smarter manner.

It seems to me that the methods in the LoggingEvent class could have the "Object message" parameter changed to "Object… messageObjects".

Am I the only one who has seen the mistake shown above? I suspect not. If the LoggerEvent class did the right thing with the messageObjects array, such as only writing out hashes of certain objects, it seems to me that this would help a lot of people and, if anyone wanted to customize the behavior, it would be clear how to do so.

Yes? No? Any other suggestions or ideas?

cheers - ray


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


Re: better way to handle objects in messages?

Posted by Nick Williams <ni...@nicholaswilliams.net>.
Check out Log4j 2. It has a significantly expanded API that does just what you're looking for.

http://logging.apache.org/log4j/2.x/

Nick

On Jun 9, 2013, at 4:36 PM, Ray Kiddy wrote:

> 
> I have done something and can see where it might be generalized to help other users of log4j. Has this issue already been discussed or dealt with in some other way?
> 
> I found a bunch of these in a large legacy application:
> 
> 	log.debug("the object "+obj+" is something something blah blah blah");
> 
> The problem, of course, is that regardless of whether the Level will lead to the statement being logged out, the obj's toString() method gets called. This can lead, at times, to quite a bit of work. And that works gets thrown away almost all of the time. So I have written a class that I send all of my log statements through. That class has methods:
> 
> 	static void info(Logger log, Object… objs);
> 	static void debug(Logger log, Object… objs);
> 	static void error(Logger log, Object… objs);
> 
> In the info() method, the Level is checked. If it is going to print, then the obis are iterated through. And for some kinds of objects we call toString but some objects are handled in a smarter manner.
> 
> It seems to me that the methods in the LoggingEvent class could have the "Object message" parameter changed to "Object… messageObjects".
> 
> Am I the only one who has seen the mistake shown above? I suspect not. If the LoggerEvent class did the right thing with the messageObjects array, such as only writing out hashes of certain objects, it seems to me that this would help a lot of people and, if anyone wanted to customize the behavior, it would be clear how to do so.
> 
> Yes? No? Any other suggestions or ideas?
> 
> cheers - ray
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>