Skip to content

Commit

Permalink
Merge pull request #33 from sndyuk/feature_marker_filter
Browse files Browse the repository at this point in the history
Add simple marker filter for appender
  • Loading branch information
sndyuk authored Feb 22, 2019
2 parents cf2d20c + 394b7fb commit b0ffc8a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 8 deletions.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.");

Expand All @@ -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.");
Expand Down Expand Up @@ -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<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
Expand All @@ -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...
}
}
15 changes: 14 additions & 1 deletion src/test/resources/logback-appenders.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,20 @@

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern><![CDATA[%date{HH:mm:ss.SSS} [%thread] %-5level %logger{15}#%line %X{req.requestURI} %msg\n]]></pattern>
<pattern><![CDATA[%date{HH:mm:ss.SSS} [%thread] %marker %-5level %logger{15}#%line %X{req.requestURI} %msg\n]]></pattern>
</encoder>
</appender>

<appender name="SECURITY_ALERT" class="ch.qos.logback.core.ConsoleAppender">
<!-- The simple marker filter instead of writing complex configuration with EvaluatorFilter -->
<filter class="ch.qos.logback.more.appenders.filter.AppendersMarkerFilter">
<marker>SECURITY_ALERT</marker>
</filter>

<target>System.err</target>

<encoder>
<pattern><![CDATA[%date{HH:mm:ss.SSS} [%thread] %marker %-5level %logger{15}#%line %X{req.requestURI} %msg\n]]></pattern>
</encoder>
</appender>

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

<include resource="logback-appenders.xml" />

<logger name="ch.qos.logback.more.appenders.LogbackAppenderTest" level="TRACE">
<logger name="ch.qos.logback.more.appenders.LogbackAppenderTest" level="TRACE" additivity="true">
<appender-ref ref="FLUENCY" />
<appender-ref ref="FLUENT" />
<appender-ref ref="DYNAMODB" />
<appender-ref ref="SECURITY_ALERT" />
</logger>

<root>
Expand Down

0 comments on commit b0ffc8a

Please sign in to comment.