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 Brett Birschbach <br...@nsighttel.com> on 2007/01/23 23:07:13 UTC

Logger (not Appender) Filter

Is there a good reason why the default log4j implementation does not allow
for a filter at the Logger level? I know we can add filters to Appenders,
but there are certain scenarios where this can be quite wasteful on
resources. Perhaps I am attacking this situation all wrong? 

Scenario:
-Using code developed by a different company. 
-The code from package com.xyz.dostuff generates a log.error, including the
stack trace, whenever an exception occurs. 
-If the Exception descends from a certain type of exception, I do not want
the exception to clutter up the log with the stack trace, nor be logged at
the error level.
-The root logger has 3 appenders (file, console, and email) 

Possible Solution: 
-Add a filter to all 3 appenders to filter out the undesired log.error's
Cons: 
-Repeatitive declarations (adding filter to all 3 appenders) 
-Every single log statement that goes through the root logger must go
through the filter
-For every single log statment, the filter is executed 3 times, once for
each filter

Proposed Solution: 
-Add a filter (not possible using default log4j) to the specific logger
com.xyz.dostuff
Pros: 
-Only need to add the filter to one logger vs. three appenders 
-Only log messages from the specific package com.xyz.dostuff must pass
through the filter
-The filter is executed only once for each log statement.

1) Am I overlooking this functionality, and is it already built into log4j?
2) Am I looking at this problem completely wrong, and is there a better
approach?
3) If 1) and 2) are no, are there any drawbacks to me wrapping the
org.apache.log4j.Logger class and adding the functionality for filters? I
assume I would need to override the default configuration classes as well?

-- 
View this message in context: http://www.nabble.com/Logger-%28not-Appender%29-Filter-tf3077715.html#a8550568
Sent from the Log4j - Users mailing list archive at Nabble.com.


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


RE: Logger (not Appender) Filter

Posted by Brett Birschbach <br...@nsighttel.com>.
Thank you very much for your response Ron.

Funny that you should mention the multi-appender idea.  I've already gone
down that road.  I rolled my own appender class that mimics appender-ref
functionality of Ansynchronous appender, but leaves out the rest.  I've
tested the appender, and it works just like you would think.

The resulting configuration is llike you said:

  <appender name="MULTI_ROOT"
class="com.XXXXXXXXX.log4j.appender.MultiAppender">
     <appender-ref ref="DEFAULTFILE"/>
     <appender-ref ref="CONSOLE"/>
     <appender-ref ref="SMTP_ERROR"/>
  </appender>
  <logger name="com.xyz.dostuff" additivity="false">
     <appender-ref ref="MULTI_ROOT"/>
  </logger>
  <root>
      <appender-ref ref="MULTI_ROOT"/>
  </root>

On the surface the solution looks good.  However, let's say I want to add
another appender for all classes in com.xyz

  <logger name="com.xyz" additivity="true">
     <appender-ref ref="XYZFILE"/>
  </logger>

Since additivity="false" for com.xyz.dostuff, if I want the output from
com.xyz.dostuff to go to XYZFILE appender as well, now I need to add the
appender to both loggers:

 <logger name="com.xyz.dostuff" additivity="false">
     <appender-ref ref="MULTI_ROOT"/>
     <appender-ref ref="XYZFILE"/>
 </logger>

  Not only am I duplicating appender references, but now I have the same
problem as originally - I have to filter both the XYZFILE appender and the
MULTI_ROOT appender.  Instead of just filtering log messages once, I have to
filter them twice, once for each filter.  Furthermore, instead of simply
filtering logging events from the com.xyz.dostuff package, now I am
filtering all logging events from all com.xyz.* packages.




Gallagher, Ron wrote:
> 
> Appenders can be nested.  Simply attach an appender "A" to the logger
> for "com.xyz.dostuff".  The configure appender "A" with the filter
> you've described and three nested appenders, one each for the file,
> console and email.
> ...
> The configuration above utilizes the AsyncAppender class which is the
> only class I found in the core log4j libraries that implements both the
> Appender and AppenderAttachable interfaces.  If you don't want to
> utilize the asynchronous aspects of this appender, you'll either have to
> roll your own non-asynchronous appender or search the various
> contributions to see if someone has already developed one.
> 
> Ron Gallagher 
> Cingular Wireless
> 
> 
> -----Original Message-----
> From: Brett Birschbach [mailto:brett.birschbach@nsighttel.com] 
> Sent: Tuesday, January 23, 2007 5:07 PM
> To: log4j-user@logging.apache.org
> Subject: Logger (not Appender) Filter
> 
> 
> Is there a good reason why the default log4j implementation does not
> allow
> for a filter at the Logger level? I know we can add filters to
> Appenders,
> but there are certain scenarios where this can be quite wasteful on
> resources. Perhaps I am attacking this situation all wrong? 
> 
> Scenario:
> -Using code developed by a different company. 
> -The code from package com.xyz.dostuff generates a log.error, including
> the
> stack trace, whenever an exception occurs. 
> -If the Exception descends from a certain type of exception, I do not
> want
> the exception to clutter up the log with the stack trace, nor be logged
> at
> the error level.
> -The root logger has 3 appenders (file, console, and email) 
> 
> Possible Solution: 
> -Add a filter to all 3 appenders to filter out the undesired log.error's
> Cons: 
> -Repeatitive declarations (adding filter to all 3 appenders) 
> -Every single log statement that goes through the root logger must go
> through the filter
> -For every single log statment, the filter is executed 3 times, once for
> each filter
> 
> Proposed Solution: 
> -Add a filter (not possible using default log4j) to the specific logger
> com.xyz.dostuff
> Pros: 
> -Only need to add the filter to one logger vs. three appenders 
> -Only log messages from the specific package com.xyz.dostuff must pass
> through the filter
> -The filter is executed only once for each log statement.
> 
> 1) Am I overlooking this functionality, and is it already built into
> log4j?
> 2) Am I looking at this problem completely wrong, and is there a better
> approach?
> 3) If 1) and 2) are no, are there any drawbacks to me wrapping the
> org.apache.log4j.Logger class and adding the functionality for filters?
> I
> assume I would need to override the default configuration classes as
> well?
> 
> -- 
> View this message in context:
> http://www.nabble.com/Logger-%28not-Appender%29-Filter-tf3077715.html#a8
> 550568
> Sent from the Log4j - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> 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
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Logger-%28not-Appender%29-Filter-tf3077715.html#a8563114
Sent from the Log4j - Users mailing list archive at Nabble.com.


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


RE: Logger (not Appender) Filter

Posted by "Gallagher, Ron" <ro...@cingular.com>.
Brett --

I can't address the reasons for why filters aren't allowed at the logger
level, but I can offer you a solution to your problem.

Appenders can be nested.  Simply attach an appender "A" to the logger
for "com.xyz.dostuff".  The configure appender "A" with the filter
you've described and three nested appenders, one each for the file,
console and email.

Here's a basic configuration.

  <appender name="Main.CustomApp.Appender"
           class="org.apache.log4j.AsyncAppender">
    <filter class="com.mycompany.MySpecialFilter"/>
    <appender-ref ref="CustomApp.FileAppender"/>
    <appender-ref ref="CustomApp.ConsoleAppender"/>
    <appender-ref ref="CustomApp.EmailAppender"/>
  </appender>
  <appender name="CustomApp.FileAppender" class="???">
    <!-- additional configuration goes here -->
  </appender>
  <appender name="CustomApp.ConsoleAppender" class="???">
    <!-- additional configuration goes here -->
  </appender>
  <appender name="CustomApp.EmailAppender" class="???">
    <!-- additional configuration goes here -->
  </appender>
  <logger name="com.xyz.dostuff" additivity="false">
    <level value="info"/>
    <appender-ref ref="Main.CustomApp.Appender"/>
  </logger>

The key is the configuration of the appender "Main.CustomApp.Appender".
This appender does nothing more than delegate to any appenders that are
attached to it.  In the configuration above, the
"Main.CustomApp.Appender" has three attached appenders.  However, before
any logging events are delegated to the attached appenders, the filters
are all interrogated to determine whether the delegation should occur.
With this approach, you configure your filter once, and the filter's
decision is applied to 3 separate filters.

The configuration above utilizes the AsyncAppender class which is the
only class I found in the core log4j libraries that implements both the
Appender and AppenderAttachable interfaces.  If you don't want to
utilize the asynchronous aspects of this appender, you'll either have to
roll your own non-asynchronous appender or search the various
contributions to see if someone has already developed one.

Ron Gallagher 
Cingular Wireless


-----Original Message-----
From: Brett Birschbach [mailto:brett.birschbach@nsighttel.com] 
Sent: Tuesday, January 23, 2007 5:07 PM
To: log4j-user@logging.apache.org
Subject: Logger (not Appender) Filter


Is there a good reason why the default log4j implementation does not
allow
for a filter at the Logger level? I know we can add filters to
Appenders,
but there are certain scenarios where this can be quite wasteful on
resources. Perhaps I am attacking this situation all wrong? 

Scenario:
-Using code developed by a different company. 
-The code from package com.xyz.dostuff generates a log.error, including
the
stack trace, whenever an exception occurs. 
-If the Exception descends from a certain type of exception, I do not
want
the exception to clutter up the log with the stack trace, nor be logged
at
the error level.
-The root logger has 3 appenders (file, console, and email) 

Possible Solution: 
-Add a filter to all 3 appenders to filter out the undesired log.error's
Cons: 
-Repeatitive declarations (adding filter to all 3 appenders) 
-Every single log statement that goes through the root logger must go
through the filter
-For every single log statment, the filter is executed 3 times, once for
each filter

Proposed Solution: 
-Add a filter (not possible using default log4j) to the specific logger
com.xyz.dostuff
Pros: 
-Only need to add the filter to one logger vs. three appenders 
-Only log messages from the specific package com.xyz.dostuff must pass
through the filter
-The filter is executed only once for each log statement.

1) Am I overlooking this functionality, and is it already built into
log4j?
2) Am I looking at this problem completely wrong, and is there a better
approach?
3) If 1) and 2) are no, are there any drawbacks to me wrapping the
org.apache.log4j.Logger class and adding the functionality for filters?
I
assume I would need to override the default configuration classes as
well?

-- 
View this message in context:
http://www.nabble.com/Logger-%28not-Appender%29-Filter-tf3077715.html#a8
550568
Sent from the Log4j - Users mailing list archive at Nabble.com.


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