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 Bruno Melloni <Br...@chickasaw.net> on 2010/01/15 17:37:58 UTC

Split logging by class/package

All examples on how to split logging across two appenders seem to be 'by level', and the XML configuration only has one set of <category> definitions rather than one set per appender.  I need to split logging by both class and level:


-          Default log:  Standard logging for the application, including log entries for 'MyClass' at INFO level and above.

-          MyClass log:  Entries for MyClass only, at DEBUG level.  It would be acceptable if it also contained MyClass higher logging levels.

My need seems to be pretty standard.  Is there a simple way to do this?

The only solution I found (but not tried) seems too complicated to be right:


-          Setup the category for MyClass at DEBUG level.

-          Create a MyClass appender in log4j.xml.

-          Write a custom Filter that only accepts MyClass log entries of DEBUG level.  Add the filter to the MyClass appender.

-          Write a custom Filter that accepts all log entries except MyClass DEBUG entries.  Add the filter to the default appender.

-          Since I am using jBoss' logging facility, place these classes 'somewhere' that puts them in jBoss' classpath.  Where would that be?

Any guidance would be greatly appreciated.

RE: Split logging by class/package

Posted by Bruno Melloni <Br...@chickasaw.net>.
Thanks for reminding me of the additivity flag.  It doesn't solve the problem 100% but allowed me to come pretty close in a very easy way:

- Created MYCLASS appender that writes to MyClass.log.
- Created a <category> entry as follows:

   <category name="mypackage.MyClass" additivity="false">
      <priority value="DEBUG"/>
      <appender-ref ref="MYCLASS"/>
   </category>

This separated all of the output of MyClass to MyClass.log.  What remains to be done is to make MyClass' log entries of level INFO and higher return to the main log.  

I wish we could just define a second <category> entry for "mypackage.MyClass" - with priority="INFO" and appender-ref pointing to the main appender.  But log4j isn't there yet.

bruno

-----Original Message-----
From: Brett Randall [mailto:javabrett@gmail.com] 
Sent: Sunday, January 17, 2010 3:36 AM
To: Log4J Users List
Subject: Re: Split logging by class/package

Bruno, have you tried setting the additivity flag for the MyClass logger to
"false"? Not sure if it meets all your needs, but it would allow you to
attach an appender to the MyClass logger and prevent propagation to ancestor
loggers and their appenders.

Brett

On Sat, Jan 16, 2010 at 3:37 AM, Bruno Melloni
<Br...@chickasaw.net>wrote:

> All examples on how to split logging across two appenders seem to be 'by
> level', and the XML configuration only has one set of <category> definitions
> rather than one set per appender.  I need to split logging by both class and
> level:
>
>
> -          Default log:  Standard logging for the application, including
> log entries for 'MyClass' at INFO level and above.
>
> -          MyClass log:  Entries for MyClass only, at DEBUG level.  It
> would be acceptable if it also contained MyClass higher logging levels.
>
> My need seems to be pretty standard.  Is there a simple way to do this?
>
> The only solution I found (but not tried) seems too complicated to be
> right:
>
>
> -          Setup the category for MyClass at DEBUG level.
>
> -          Create a MyClass appender in log4j.xml.
>
> -          Write a custom Filter that only accepts MyClass log entries of
> DEBUG level.  Add the filter to the MyClass appender.
>
> -          Write a custom Filter that accepts all log entries except
> MyClass DEBUG entries.  Add the filter to the default appender.
>
> -          Since I am using jBoss' logging facility, place these classes
> 'somewhere' that puts them in jBoss' classpath.  Where would that be?
>
> Any guidance would be greatly appreciated.
>

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


Re: Split logging by class/package

Posted by Brett Randall <ja...@gmail.com>.
Bruno, have you tried setting the additivity flag for the MyClass logger to
"false"? Not sure if it meets all your needs, but it would allow you to
attach an appender to the MyClass logger and prevent propagation to ancestor
loggers and their appenders.

Brett

On Sat, Jan 16, 2010 at 3:37 AM, Bruno Melloni
<Br...@chickasaw.net>wrote:

> All examples on how to split logging across two appenders seem to be 'by
> level', and the XML configuration only has one set of <category> definitions
> rather than one set per appender.  I need to split logging by both class and
> level:
>
>
> -          Default log:  Standard logging for the application, including
> log entries for 'MyClass' at INFO level and above.
>
> -          MyClass log:  Entries for MyClass only, at DEBUG level.  It
> would be acceptable if it also contained MyClass higher logging levels.
>
> My need seems to be pretty standard.  Is there a simple way to do this?
>
> The only solution I found (but not tried) seems too complicated to be
> right:
>
>
> -          Setup the category for MyClass at DEBUG level.
>
> -          Create a MyClass appender in log4j.xml.
>
> -          Write a custom Filter that only accepts MyClass log entries of
> DEBUG level.  Add the filter to the MyClass appender.
>
> -          Write a custom Filter that accepts all log entries except
> MyClass DEBUG entries.  Add the filter to the default appender.
>
> -          Since I am using jBoss' logging facility, place these classes
> 'somewhere' that puts them in jBoss' classpath.  Where would that be?
>
> Any guidance would be greatly appreciated.
>