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: parent logger to filter child logger by level
Date Tue, 18 Dec 2007 19:04:43 GMT

On Dec 18, 2007, at 8:39 AM, Mak, Howard wrote:

> (I)
>
> Yes, I did not know about the appender threshold.  Thanks for the  
> replies ... the information was exactly what I was looking for!
>
> Is there anything that does the "complement" of this threshold  
> (i.e., appender should keep log only if it falls outside [instead of  
> inside] the threshold)?  This would be most useful to route FATAL/ 
> ERROR/WARN to a stdout ConsoleAppender and all other logs to a  
> stdout ConsoleAppender.
>

The extras companion for log4j (http://logging.apache.org/log4j/companions/extras 
) contains a LevelRangeFilter that could be used for that purpose.  I  
assume there is a typo in your statement since you appear to be  
wanting to route both classes of messages to a "stdout  
ConsoleAppender".  Filters are applied to Appenders and are only  
supported in with the DOMConfigurator.

> (II)
>
> I now have a different question:
>
> Suppose my logger tree is:
>    rootLogger
>    foo
>    bar
> and
>    addivity is default (true)
>
> How can I configure the following?
>
>    "foo" logger level is ALL
>
>    "bar" logger level is ALL
>
>    File A contains all "foo" logs and just FATAL/ERROR/WARN "bar" logs
>
>    File B contains all "bar" logs
>
> Do I need to write some custom appender to achieve this?
>

If you'd need to write any custom code it would be a custom filter.   
However, that seems within the capabilities of the filters in the  
extras companion.

The essentials are:

Attach the appender for File B to bar, attach the appender for File B  
to rootLogger and add a filter (either an ExpressionFilter or a  
LoggerMatchFilter and a LevelRangeFilter), something like:

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'
                      debug="true">

   <appender name="A" class="org.apache.log4j.FileAppender">
     <param name="Append" value="false" />
     <param name="File"   value="file_a.log" />
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern"
	value="%p %c - %m\n"/>
     </layout>
     <filter class="org.apache.log4j.filter.ExpressionFilter">
        <!--  spaces in expression value are significant  -->
        <param name="expression" value="( LOGGER != bar ) || ( LEVEL  
 >= WARN )"/>
     </filter>
     <filter class="org.apache.log4j.filter.DenyAllFilter"/>
   </appender>

   <appender name="B" class="org.apache.log4j.FileAppender">
     <param name="Append" value="false" />
     <param name="File"   value="file_b.log" />
   </appender>


   <root>
     <appender-ref ref="A" />
   </root>
   <logger name="bar">
      <appender-ref ref="B"/>
   </logger>
</log4j:configuration>



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


Mime
View raw message