blob: 8c20563ab1ab88635f90a7ed1b0cd360240b1dac [file] [log] [blame]
William Kurkianbde6fc92018-07-13 17:19:58 -04001/*
2 * Copyright 2018- Cisco
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package filter;
17
18import java.util.Arrays;
19
20import org.slf4j.Marker;
21
22import ch.qos.logback.classic.spi.ILoggingEvent;
23import ch.qos.logback.core.filter.AbstractMatcherFilter;
24import ch.qos.logback.core.spi.FilterReply;
25
26import org.slf4j.Marker;
27import org.slf4j.MarkerFactory;
28
29public class MarkerFilter extends AbstractMatcherFilter<ILoggingEvent> {
30
31 Marker markerToMatch;
32
33 /*
34 * (non-Javadoc)
35 *
36 * @see ch.qos.logback.core.filter.Filter#decide(java.lang.Object)
37 */
38 @Override
39 public FilterReply decide(ILoggingEvent event) {
40 if (!isStarted()) {
41 return FilterReply.NEUTRAL;
42 }
43 Marker marker = event.getMarker();
44 if (marker == null) {
45 return onMismatch;
46 }
47
48 if (markerToMatch.contains(marker)) {
49 return onMatch;
50 } else {
51 return onMismatch;
52 }
53 }
54
55 /**
56 * The marker to match in the event.
57 *
58 * @param markerToMatch
59 */
60 public void setMarker(String markerStr) {
61 if (markerStr != null) {
62 Marker marker = MarkerFactory.getMarker(markerStr);
63 this.markerToMatch = marker;
64 }
65 }
66
67 /*
68 * (non-Javadoc)
69 *
70 * @see ch.qos.logback.core.filter.Filter#start()
71 */
72 @Override
73 public void start() {
74 if (this.markerToMatch != null) {
75 super.start();
76 }
77 }
78}