-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from sndyuk/feature_marker_filter
Add simple marker filter for appender
- Loading branch information
Showing
4 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/ch/qos/logback/more/appenders/filter/AppendersMarkerFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package ch.qos.logback.more.appenders.filter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.slf4j.Marker; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.filter.AbstractMatcherFilter; | ||
import ch.qos.logback.core.spi.FilterReply; | ||
|
||
/** | ||
* Simple marker filter for Appender. | ||
* | ||
* <pre> | ||
* {@code | ||
* <filter class="ch.qos.logback.more.appenders.filter.AppendersMarkerFilter"> | ||
* <marker>NOTIFY_ADMIN</marker> | ||
* <!-- Accept multiple markers --> | ||
* <marker>TRANSACTION_FAILURE</marker> | ||
* <!--[Optional] OnMismatch/OnMatch--> | ||
* <OnMismatch>DENY</OnMismatch> | ||
* <OnMatch>NEUTRAL</OnMatch> | ||
* </filter> | ||
* } | ||
* </pre> | ||
* | ||
* @author sndyuk | ||
*/ | ||
public class AppendersMarkerFilter extends AbstractMatcherFilter<ILoggingEvent> { | ||
|
||
private List<String> markers = new ArrayList<String>(); | ||
|
||
public AppendersMarkerFilter() { | ||
setOnMatch(FilterReply.NEUTRAL); | ||
setOnMismatch(FilterReply.DENY); | ||
} | ||
|
||
public void addMarker(String markerStr) { | ||
markers.add(markerStr); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
if (!markers.isEmpty()) { | ||
super.start(); | ||
} else { | ||
String name = this.getName(); | ||
addError("No marker set for filter " + (name != null ? name : this.getClass())); | ||
} | ||
} | ||
|
||
@Override | ||
public FilterReply decide(ILoggingEvent event) { | ||
if (!isStarted()) { | ||
return FilterReply.NEUTRAL; | ||
} | ||
Marker eventsMarker = event.getMarker(); | ||
if (eventsMarker == null) { | ||
return onMismatch; | ||
} | ||
|
||
for (String markerStr : markers) { | ||
if (eventsMarker.contains(markerStr)) { | ||
return onMatch; | ||
} | ||
} | ||
return onMismatch; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters