[logback-dev] [Bug 36] add filter for simple filtering on level

bugzilla-daemon at pixie.qos.ch bugzilla-daemon at pixie.qos.ch
Thu Jan 4 07:09:02 CET 2007


http://bugzilla.qos.ch/show_bug.cgi?id=36





------- Comment #1 from sdavids at gmx.de  2007-01-04 07:09 -------
/*******************************************************************************
 * Copyright (c) 2007 Sebastian Davids.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:
 *     Sebastian Davids - initial API and implementation

*******************************************************************************/
package name.davids.sebastian.logback.filter;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;

/**
 * Filter for filtering based on the logging event's level.
 * <p>
 * This filter denies events not having a level equal or greater than the one
supplied via {@link #setLevel(String)}.
 * </p>
 * <h3>Usage Examples</h3>
 * <h4>Programmatic</h4>
 * <pre> LevelFilter filter = new LevelFilter();
 * filter.setLevel(&quot;INFO&quot;);</pre>
 * <h4>Configuration</h4>
 * <pre> &lt;configuration&gt;
 *   &lt;appender ...&gt;
 *     &lt;filter
class=&quot;name.davids.sebastian.logback.filter.LevelFilter&quot;&gt;
 *       &lt;level&gt;INFO&lt;/level&gt;
 *     &lt;/filter&gt;
 *   &lt;/appender&gt;
 * &lt;/configuration&gt;</pre>
 * 
 * @author Sebastian Davids, <a
href="mailto:sebastian at davids.name">sebastian at davids.name</a>
 */
// see http://logback.qos.ch/manual/filters.html
public class LevelFilter extends Filter {

    /** The level or <code>null</code> if the default level should be used. */
    private Level level;

    /** {@inheritDoc} */
    @Override
    public FilterReply decide(Object eventObject) {
        if (!(eventObject instanceof LoggingEvent)) return FilterReply.NEUTRAL;
        if (!isStarted()) return FilterReply.NEUTRAL;
        LoggingEvent event = (LoggingEvent) eventObject;
        return event.getLevel().isGreaterOrEqual(level) ? FilterReply.NEUTRAL :
FilterReply.DENY;
    }

    /**
     * Sets the level for this filter.
     * 
     * @param level
     *            the level or <code>null</code> if the default level should be
used
     * @see Level#toLevel(String)
     */
    public void setLevel(String level) {
        this.level = Level.toLevel(level);
    }

    /** {@inheritDoc} */
    @Override
    public void start() {
        if (level == null) level = Level.DEBUG;
        super.start();
    }
}


-- 
Configure bugmail: http://bugzilla.qos.ch/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.



More information about the logback-dev mailing list