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 "Faine, Mark R. (MSFC-NNM04AA02C)[SAIC]" <ma...@nasa.gov> on 2007/08/31 17:13:18 UTC

Pretty Layout of logging by class & method

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

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


RE: Pretty Layout of logging by class & method

Posted by "Faine, Mark R. (MSFC-NNM04AA02C)[SAIC]" <ma...@nasa.gov>.
Thanks for the suggestion.  I may attempt this.  The application is
simple and yes I have considered some of the concerns you have laid out
but I believe the utility of this would outweigh the possible problems I
could have with it. I may even gain a little more in-depth knowledge of
log4j in the process.

-Mark

-----Original Message-----
From: Curt Arnold [mailto:carnold@apache.org] 
Sent: Friday, August 31, 2007 10:54 AM
To: Log4J Users List
Subject: Re: Pretty Layout of logging by class & method


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

>
> 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


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


Re: Pretty Layout of logging by class & method

Posted by Curt Arnold <ca...@apache.org>.
On Aug 31, 2007, at 10:13 AM, Faine, Mark R. (MSFC-NNM04AA02C)[SAIC]  
wrote:

>
> 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


Re: Pretty Layout of logging by class & method

Posted by "James A. N. Stauffer" <st...@gmail.com>.
You might be able to push a space character with NDC (and pop it when
you exit).  Then print the NDC as the first part of the message.

On 8/31/07, Faine, Mark R. (MSFC-NNM04AA02C)[SAIC] <ma...@nasa.gov> wrote:
>
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>


-- 
James A. N. Stauffer        http://www.geocities.com/stauffer_james/
Are you good? Take the test at http://www.livingwaters.com/good/

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


RE: Pretty Layout of logging by class & method

Posted by "Faine, Mark R. (MSFC-NNM04AA02C)[SAIC]" <ma...@nasa.gov>.
The question really just goes to layout though I suppose logger.log will
be called in various locations in nearly every method but also
specifically at the start and end of each method.  My first thought was
a layout class that indented logger.log calls within methods but there
is the question of how to know the level of indention for each
logger.log call.  I also considered the XML layout and applying XSLT but
that seems far too involved a solution.

Here is another example that looks a little more accurate.  (only an
example, not based on real output)

08:50:49,661 DEBUG Class1:29     Inside Main Method Class1
08:50:50,663 DEBUG Class1:30     Calling Sub method Class1
08:50:51,663 DEBUG Class1:131      Inside Sub Method Class1
08:50:52,663 DEBUG Class:135         Some other debugging statement...
08:50:53,664 DEBUG Class1:138        Calling method in another class...
08:50:54,665 DEBUG   Class2:253      Inside  Method of Class2
08:50:55,666 DEBUG   Class2:300        Calling Sub method in Class2
08:50:56,667 DEBUG   Class2:340         Some other debugging
statement...
08:50:57,668 DEBUG   Class2:345        Success sub-method of Class2
08:50:58,669 DEBUG   Class2:346      Success method of Class2
08:50:59,663 DEBUG Class1:245      Success Sub Method of Class1
08:51:00,663 DEBUG Class1:245    Success Main Method of Class1


-----Original Message-----
From: Matthew Kemp [mailto:mattkemp@gmail.com] 
Sent: Friday, August 31, 2007 10:19 AM
To: Log4J Users List
Subject: Re: Pretty Layout of logging by class & method

Are you saying that you want one logger.log() call to print the entire
stack trace? Or do you want logger.log() calls exiting and entering each
method?

On 8/31/07, Faine, Mark R. (MSFC-NNM04AA02C)[SAIC] <ma...@nasa.gov>
wrote:
>
>
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

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


Re: Pretty Layout of logging by class & method

Posted by Matthew Kemp <ma...@gmail.com>.
Are you saying that you want one logger.log() call to print the entire stack
trace? Or do you want logger.log() calls exiting and entering each method?

On 8/31/07, Faine, Mark R. (MSFC-NNM04AA02C)[SAIC] <ma...@nasa.gov>
wrote:
>
>
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>