logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mikael Ståldal <mikael.stal...@univits.com>
Subject Re: Different levels to different appenders
Date Mon, 03 Mar 2008 10:14:13 GMT
Mikael Ståldal skrev:
>> then set the logger levels for both hierarchies to DEBUG, and write 
>> yourself a simple custom Filter implementation that accepts/denies 
>> events based on your logic, and attach a separate configured instance 
>> of that filter to each of the appenders (accepting/denying as the case 
>> may be above). 
> 
> I have done that, and it seems to work. I'm not sure how it would affect 
> the performance though.

Here is the filter code I use. I hereby donate this code to the log4j project. I suggest 
that it's included in the org.apache.log4j.varia package.


import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.Filter;

/**
  * Filter to ACCEPT log messages based on Level <strong>and</strong> Logger name
prefix.
  *
  * Parameters:
  * <ul>
  * <li>threshold - the Level threshold</li>
  * <li>prefix - the Logger name prefix, use the empty string to match all Loggers
  * (including the root Logger).</li>
  * <ul>
  *
  * This filter is NEUTRAL if the Logger name prefix doesn't match, and will ACCEPT or
  * DENY based on Level threshold. You can chain several instances of this filter.
  *
  * @author Mikael Ståldal
  */
public class LevelAndPrefixFilter extends Filter {

     Level threshold;
     String prefix;

     @Override
     public int decide(LoggingEvent event) {
         if (!event.getLoggerName().startsWith(prefix)) {
             return NEUTRAL;
         }

         if (event.getLevel().isGreaterOrEqual(threshold)) {
             return ACCEPT;
         } else {
             return DENY;
         }
     }

     public Level getThreshold() {
         return threshold;
     }

     public void setThreshold(Level threshold) {
         this.threshold = threshold;
     }

     public String getPrefix() {
         return prefix;
     }

     public void setPrefix(String prefix) {
         this.prefix = prefix;
     }

}

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