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:[EMAIL PROTECTED]
Sent: Tuesday, January 23, 2007 5:07 PM
To: [email protected]
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]