logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: Pretty Layout of logging by class & method
Date Fri, 31 Aug 2007 15:53:43 GMT

On Aug 31, 2007, at 10:13 AM, Faine, Mark R. (MSFC-NNM04AA02C)[SAIC]  

> I'd like for Log4J to print the log messages with some sort of
> indentation, preferably by Class and Method
> Something like:
> Start Class1
>   Inside Main method...
>   Calling Sub method...
>     Inside Sub method...
>       Executing operations in sub method...
>     Sub Method success
>   Main Method success
> End Class1
> Start Class2
> ...
> End Class2
> Is this currently possible?
> Thanks,
> -Mark

I think the usefulness of that would break down if you had a multi- 
threaded app logging to a single log file since you'd have the indent  
level jumping all over the place.  However, maybe your app is single  
threaded or you've isolated each thread to log to a different file.   
Also if your code uses deep recursion, you could end up with a huge  
amount of leading spaces.

Next you would need to be consistent in logging method entry and  
exits.  java.util.logging.Logger provides entering, exiting and  
throwing methods that prepare standard messages for those events.  It  
would probably would be good to add corresponding methods to LogMF in  
the extras companion, but that wouldn't help you since it would not  
address your indent issue.

Probably your best bet is to write your own helper class with static  
methods paralleling the java.util.logging.Logger.entering et al  
methods.  If you are not already using the NDC for something else  
(NDC is a thread-local logging context stack), then your entering  
methods could call NDC.getDepth(), create an appropriate indent  
string (depth*3 spaces for example) and then push it using NDC.push 
(), your exiting and throwing methods would call NDC.pop().  Your  
pattern layout should contain a "%x" where you want to NDC to appear.

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

View raw message