blob: 9887409f563dde750b865434a5437e5c885823f4 [file] [log] [blame]
William Kurkianbde6fc92018-07-13 17:19:58 -04001/*
William Kurkian1bedb412018-07-19 12:55:41 -04002* 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*/
William Kurkianbde6fc92018-07-13 17:19:58 -040016package 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 /*
William Kurkian1bedb412018-07-19 12:55:41 -040034 * (non-Javadoc)
35 *
36 * @see ch.qos.logback.core.filter.Filter#decide(java.lang.Object)
37 */
William Kurkianbde6fc92018-07-13 17:19:58 -040038 @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 /**
William Kurkian1bedb412018-07-19 12:55:41 -040056 * The marker to match in the event.
57 *
58 * @param markerToMatch
59 */
William Kurkianbde6fc92018-07-13 17:19:58 -040060 public void setMarker(String markerStr) {
61 if (markerStr != null) {
62 Marker marker = MarkerFactory.getMarker(markerStr);
63 this.markerToMatch = marker;
64 }
65 }
66
67 /*
William Kurkian1bedb412018-07-19 12:55:41 -040068 * (non-Javadoc)
69 *
70 * @see ch.qos.logback.core.filter.Filter#start()
71 */
William Kurkianbde6fc92018-07-13 17:19:58 -040072 @Override
73 public void start() {
74 if (this.markerToMatch != null) {
75 super.start();
76 }
77 }
78}