diff --git a/src/main/java/ch/qos/logback/more/appenders/filter/AppendersMarkerFilter.java b/src/main/java/ch/qos/logback/more/appenders/filter/AppendersMarkerFilter.java new file mode 100644 index 0000000..078ab6f --- /dev/null +++ b/src/main/java/ch/qos/logback/more/appenders/filter/AppendersMarkerFilter.java @@ -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. + * + *
+ * {@code
+ * 
+ *    NOTIFY_ADMIN
+ *    
+ *    TRANSACTION_FAILURE
+ *    
+ *    DENY
+ *    NEUTRAL
+ *  
+ * }
+ * 
+ * + * @author sndyuk + */ +public class AppendersMarkerFilter extends AbstractMatcherFilter { + + private List markers = new ArrayList(); + + 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; + } +} diff --git a/src/test/java/ch/qos/logback/more/appenders/LogbackAppenderTest.java b/src/test/java/ch/qos/logback/more/appenders/LogbackAppenderTest.java index 1b4a817..7ba3b53 100644 --- a/src/test/java/ch/qos/logback/more/appenders/LogbackAppenderTest.java +++ b/src/test/java/ch/qos/logback/more/appenders/LogbackAppenderTest.java @@ -23,7 +23,7 @@ import org.slf4j.LoggerFactory; import org.slf4j.MDC; import org.slf4j.Marker; -import org.slf4j.helpers.BasicMarkerFactory; +import org.slf4j.MarkerFactory; import ch.qos.logback.more.appenders.marker.MapMarker; public class LogbackAppenderTest { @@ -50,7 +50,7 @@ public void logMdc() throws InterruptedException { @Test public void logMarker() throws InterruptedException { - Marker sendEmailMarker = new BasicMarkerFactory().getMarker("SEND_EMAIL"); + Marker sendEmailMarker = MarkerFactory.getMarker("SEND_EMAIL"); LOG.debug(sendEmailMarker, "Test the marker 1."); LOG.debug(sendEmailMarker, "Test the marker 2."); @@ -59,8 +59,8 @@ public void logMarker() throws InterruptedException { @Test public void logNestedMarker() throws InterruptedException { - Marker notifyMarker = new BasicMarkerFactory().getMarker("NOTIFY"); - Marker sendEmailMarker = new BasicMarkerFactory().getMarker("SEND_EMAIL"); + Marker notifyMarker = MarkerFactory.getMarker("NOTIFY"); + Marker sendEmailMarker = MarkerFactory.getMarker("SEND_EMAIL"); sendEmailMarker.add(notifyMarker); LOG.debug(sendEmailMarker, "Test the nested marker 1."); LOG.debug(sendEmailMarker, "Test the nested marker 2."); @@ -93,7 +93,7 @@ public void logMapMarker() throws InterruptedException { @Test public void logNestedMapMarker() throws InterruptedException { - Marker notifyMarker = new BasicMarkerFactory().getMarker("NOTIFY"); + Marker notifyMarker = MarkerFactory.getMarker("NOTIFY"); Map map = new HashMap(); map.put("key1", "value1"); map.put("key2", "value2"); @@ -104,4 +104,12 @@ public void logNestedMapMarker() throws InterruptedException { Thread.sleep(1000); // Wait a moment because these log is being appended asynchronous... } + + @Test + public void logDecidedByAppendersMarkerFilter() throws InterruptedException { + Marker alertMarker = MarkerFactory.getMarker("SECURITY_ALERT"); + LOG.debug(alertMarker, "Test alert filter."); + + Thread.sleep(1000); // Wait a moment because these log is being appended asynchronous... + } } diff --git a/src/test/resources/logback-appenders.xml b/src/test/resources/logback-appenders.xml index 1727b8e..be888c1 100644 --- a/src/test/resources/logback-appenders.xml +++ b/src/test/resources/logback-appenders.xml @@ -140,7 +140,20 @@ - + + + + + + + + SECURITY_ALERT + + + System.err + + + diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml index cb70990..4fbc5bc 100644 --- a/src/test/resources/logback.xml +++ b/src/test/resources/logback.xml @@ -4,10 +4,10 @@ - + - +