[Bug 36] New: add filter for simple filtering on level

http://bugzilla.qos.ch/show_bug.cgi?id=36 Summary: add filter for simple filtering on level Product: logback-classic Version: unspecified Platform: All OS/Version: All Status: NEW Severity: normal Priority: P3 Component: Other AssignedTo: logback-dev@qos.ch ReportedBy: sdavids@gmx.de EvaluatorFilter is nice but drags in a new dependency on Janino. Filtering on levels is the most common use case I can think of, so it should be provided out of the box. -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=36 ------- Comment #1 from sdavids@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("INFO");</pre> * <h4>Configuration</h4> * <pre> <configuration> * <appender ...> * <filter class="name.davids.sebastian.logback.filter.LevelFilter"> * <level>INFO</level> * </filter> * </appender> * </configuration></pre> * * @author Sebastian Davids, <a href="mailto:sebastian@davids.name">sebastian@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.

http://bugzilla.qos.ch/show_bug.cgi?id=36 ------- Comment #2 from sdavids@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 junit.framework.TestCase; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.spi.LoggingEvent; import ch.qos.logback.core.spi.FilterReply; /** * Test for {@link LevelFilter}. * * @author Sebastian Davids, <a href="mailto:sebastian@davids.name">sebastian@davids.name</a> */ public class LevelFilterTest extends TestCase { public void testDecideNull() { levelFilter.start(); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(null)); } public void testDecideString() { levelFilter.start(); assertEquals(FilterReply.NEUTRAL, levelFilter.decide("")); } public void testDecideNotStarted() { assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createDebugEvent())); } public void testDecideDefaultLevel() { levelFilter.start(); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createDebugEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createInfoEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createWarnEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createErrorEvent())); } public void testDecideDebugLevel() { levelFilter.setLevel("DEBUG"); //$NON-NLS-1$ levelFilter.start(); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createDebugEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createInfoEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createWarnEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createErrorEvent())); } public void testDecideInfoLevel() { levelFilter.setLevel("INFO"); //$NON-NLS-1$ levelFilter.start(); assertEquals(FilterReply.DENY, levelFilter.decide(createDebugEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createInfoEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createWarnEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createErrorEvent())); } public void testDecideWarnLevel() { levelFilter.setLevel("WARN"); //$NON-NLS-1$ levelFilter.start(); assertEquals(FilterReply.DENY, levelFilter.decide(createDebugEvent())); assertEquals(FilterReply.DENY, levelFilter.decide(createInfoEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createWarnEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createErrorEvent())); } public void testDecideErrorLevel() { levelFilter.setLevel("ERROR"); //$NON-NLS-1$ levelFilter.start(); assertEquals(FilterReply.DENY, levelFilter.decide(createDebugEvent())); assertEquals(FilterReply.DENY, levelFilter.decide(createInfoEvent())); assertEquals(FilterReply.DENY, levelFilter.decide(createWarnEvent())); assertEquals(FilterReply.NEUTRAL, levelFilter.decide(createErrorEvent())); } private LevelFilter levelFilter; /** {@inheritDoc} */ @Override protected void setUp() throws Exception { levelFilter = new LevelFilter(); } private LoggingEvent createDebugEvent() { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.DEBUG); return event; } private LoggingEvent createErrorEvent() { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.ERROR); return event; } private LoggingEvent createInfoEvent() { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.INFO); return event; } private LoggingEvent createWarnEvent() { LoggingEvent event = new LoggingEvent(); event.setLevel(Level.WARN); return event; } } -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=36 ------- Comment #3 from sdavids@gmx.de 2007-01-04 07:12 ------- Permission granted to Logback Committers to incorporate them into Logback codebase. You may move them into a more appropriate package and rename them. Please retain an attribution notice, thanks. -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=36 ------- Comment #4 from sdavids@gmx.de 2007-01-04 07:14 ------- Usage example: <?xml version="1.0" encoding="UTF-8" ?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%msg%n</pattern> </layout> <filter class="name.davids.sebastian.logback.filter.LevelFilter"> <level>INFO</level> </filter> </appender> <root> <level value="debug" /> <appender-ref ref="STDOUT" /> </root> </configuration> -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=36 noreply.sebastien@qos.ch changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |ASSIGNED ------- Comment #5 from noreply.sebastien@qos.ch 2007-01-04 14:42 ------- Hello Sebastian, Thanks a lot for your contributions! You are absolutely right on the fact that a filter on levels is a very common use case. This is very kind of you to spend time on this to help us provide a better framework! :) I've seen that you retain the copyright on the code you provided. This might be troublesome, since you haven't signed our Contributor License Agreement[1]. Of course, we will gladly include your name as the author of the classes. But we prefer to ask the contributor either to give us the full copyright of the work, or to sign our CLA. For the LevelFilter, as well as for the JMX components, we have some work in progress. Some work on our own LevelFilter class has begun after a discussion on the user mailing list about filter performances. We would very happily study and possibly integrate your work with our existing code, if you agree to the previous copyright points. Thanks again for your contributions, we appreciate that a lot :) Sébastien [1] http://logback.qos.ch/cla.txt -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=36 ------- Comment #6 from sdavids@gmx.de 2007-01-04 16:12 -------
But we prefer to ask the contributor either to give us the full copyright of the work This is what I meant by comment 3 -- you (Logback commiters/QOS) may remove the Copyright and exchange it with your own.
The only thing I'd ask for would be to include my name as the original author. -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=36 ------- Comment #7 from sdavids@gmx.de 2007-01-04 16:13 ------- To clarify: The same applies to the classes from bug 35. -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=36 ------- Comment #8 from noreply.sebastien@qos.ch 2007-01-05 10:23 ------- Ok, thanks for your answer. I'll study your code at the beginning of next week and let you know what will follow. -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=36 noreply.sebastien@qos.ch changed: What |Removed |Added ---------------------------------------------------------------------------- Status|ASSIGNED |RESOLVED Resolution| |FIXED ------- Comment #9 from noreply.sebastien@qos.ch 2007-01-10 21:34 ------- Hello Sebastian, First of all, thanks a lot for your contribution to logback :) Consequently to a discussion in the mailing list[1], we had a filter in the works when you sent your contribution. It had already passed through a few revisions[2]. Since then, it has even evolved into two filters, namely LevelFilter and ThresholdFilter. Most of the code was written by the time of your contribution and we didn't extract code from the classes you submitted. However, the situation regarding your contribution on JMX components was more useful. I've added a comment in the corresponding bugzilla entry. [1] http://www.qos.ch/pipermail/logback-user/2006-December/000070.html [2] http://tinyurl.com/yl4wag -- 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.

http://bugzilla.qos.ch/show_bug.cgi?id=36 noreply.sebastien@qos.ch changed: What |Removed |Added ---------------------------------------------------------------------------- Status|RESOLVED |CLOSED -- 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.
participants (1)
-
bugzilla-daemon@pixie.qos.ch