Complex logging of class with many members
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Chris_Duhon
Posted On:   Thursday, May 8, 2003 11:54 AM

How would one log the contents of a complex class that contains other classes as members? For example, consider: class Outter { int i; Inner a, b; } Outter outter; Logger logger; Would I would ideally like to do is something like: logger.info (outter); And then as part of logging outter, something like: logger.info ("i = " + i); logger.info (a); logger.info (b); so that the output might look like: 08 May 2003 14:26:37,165 [main] INFO Outter i = 123 08 May 2003 14:26:37,166 [main] INFO Inner foo1 08 May 2003 14:26:37,167 [main] INFO Inner foo2    More>>

How would one log the contents of a complex class that contains

other classes as members? For example, consider:

			
class Outter
{
int i;
Inner a, b;
}
Outter outter;
Logger logger;

Would I would ideally like to do is something like:
			
logger.info (outter);

And then as part of logging outter, something like:
			
logger.info ("i = " + i);
logger.info (a);
logger.info (b);

so that the output might look like:
			
08 May 2003 14:26:37,165 [main] INFO Outter i = 123
08 May 2003 14:26:37,166 [main] INFO Inner foo1
08 May 2003 14:26:37,167 [main] INFO Inner foo2

And if the Inner class had other elements to log, it would log them

individually as well, and so on. This shifts the work of knowing

how to render a class onto the class itself.



Although I understand how to install ObjectRenderers for Inner and

Outter, it is not clear how Outter's renderer would render the Inner objects,

short of searching for and invoking doRender methods on a and b.    <<Less

Re: Complex logging of class with many members

Posted By:   Christopher_Koenigsberg  
Posted On:   Thursday, May 8, 2003 12:03 PM

It may not be the best way, but I have been following a general pattern, of having an overall Logger for each class (e.g. class member field "Logger logger = Logger.getLogger(CLASSNAME)" where CLASSNAME is e.g. "this.getClass().getName()").

Also I put a "static final String mylogname = " local variable, in each method that I want to do logging from.



So I can usually write "logger.debug(mylogname + whatever)", getting the "mylogname" that is a local string, and getting the "logger" that is a member field of the class. Inner classes get their own logger, too.

About | Sitemap | Contact