Skip to content

Instantly share code, notes, and snippets.

@takumakei
Created May 24, 2011 15:50
Show Gist options
  • Save takumakei/988965 to your computer and use it in GitHub Desktop.
Save takumakei/988965 to your computer and use it in GitHub Desktop.
Logback NON-turbo filter checks whether the marker in the event matches the marker
/* MarkerFilter.java
* Logback NON-turbo filter checks whether the marker in the event matches the marker.
*
* Copyright (C) 2011 TAKUMAKei
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blogspot.takumakei.gist;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.AbstractMatcherFilter;
import ch.qos.logback.core.spi.FilterReply;
/**
* Non-turbo filter checks whether the marker in the event matches the marker specified by user.
*/
public class MarkerFilter extends AbstractMatcherFilter<ILoggingEvent> {
protected Marker markerToMatch;
/**
* Make a decision based on a logging event passed as an argument.
*/
@Override
public FilterReply decide(ILoggingEvent event) {
if (!isStarted()) { // this means no marker property specified
return FilterReply.NEUTRAL;
}
final Marker eventMarker = event.getMarker();
if (eventMarker == null) { // the event has no marker
return onMismatch;
}
if (markerToMatch.equals(eventMarker) || markerToMatch.contains(eventMarker)) {
// MATCH!
// this filter's marker exactly equals event's marker
// or
// this filter's marker contains event's marker.
return onMatch;
}
// otherwise, mismatch
return onMismatch;
}
@Override
public void start() {
if (null == markerToMatch) {
addError("The marker property must be set for ["+getName()+"].");
return;
}
super.start();
addInfo("Filter["+getName()+"] matches the marker ["+markerToMatch.getName()
+"], returns ["+onMatch+"] on match, ["+onMismatch+"] on mismatch");
}
/**
* The marker to match in the event.
* @param markerName
*/
public void setMarker(String markerName) {
markerToMatch = MarkerFactory.getMarker(markerName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment