97fc2871ded2f56572a19faaeca7f5c296ffe6f1
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
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  */
16
17 package org.openecomp.sdc.logging.logback;
18
19 import ch.qos.logback.classic.Level;
20 import ch.qos.logback.classic.spi.ILoggingEvent;
21 import ch.qos.logback.core.sift.AbstractDiscriminator;
22 import org.openecomp.sdc.logging.Markers;
23 import org.slf4j.Marker;
24
25 /**
26  * Can be used with {@link ch.qos.logback.classic.sift.SiftingAppender} to route events of different types to
27  * separate log files. For example,
28  *
29  * <pre>
30  *     &lt;configuration&gt;
31  *         &lt;appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"&gt;
32  *             &lt;discriminator class="org.openecomp.sdc.logging.logback.EventTypeDiscriminator"/&gt;
33  *             &lt;sift&gt;
34  *                  &lt;appender name="{EventType}" class="ch.qos.logback.core.rolling.RollingFileAppender"&gt;
35  *                      &lt;file&gt;${logDirectory}/${eventType}.log&lt;/file&gt;
36  *                      &lt;rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"&gt;
37  *                          &lt;fileNamePattern&gt;${logDirectory}/${eventType}.%i.log.zip&lt;/fileNamePattern&gt;
38  *                          &lt;minIndex&gt;1&lt;/minIndex&gt;
39  *                          &lt;maxIndex&gt;9&lt;/maxIndex&gt;
40  *                      &lt;/rollingPolicy&gt;
41  *                      &lt;triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"&gt;
42  *                          &lt;maxFileSize&gt;5MB&lt;/maxFileSize&gt;
43  *                      &lt;/triggeringPolicy&gt;
44  *                      &lt;encoder&gt;
45  *                          &lt;pattern&gt;${defaultPattern}&lt;/pattern&gt;
46  *                      &lt;/encoder&gt;
47  *                  &lt;/appender&gt;
48  *             &lt;/sift&gt;
49  *         &lt;/appender&gt;
50  *
51  *         &lt;root level="INFO"&gt;
52  *             &lt;appender-ref ref="SIFT" /&gt;
53  *         &lt;/root&gt;
54  *     &lt;/configuration&gt;
55  * </pre>
56  *
57  * @author evitaliy
58  * @since 21/07/2016.
59  */
60 public class EventTypeDiscriminator extends AbstractDiscriminator<ILoggingEvent> {
61
62     private static final String KEY = "eventType";
63
64     private static final String AUDIT = "Audit";
65     private static final String METRICS = "Metrics";
66     private static final String ERROR = "Error";
67     private static final String DEBUG = "Debug";
68     private static final String DEFAULT = DEBUG;
69
70     private static final int MIN_ERROR_LEVEL = Level.WARN_INT;
71     private static final int MAX_ERROR_LEVEL = Level.ERROR_INT;
72     private static final int DEFAULT_LEVEL = Level.DEBUG_INT;
73
74     @Override
75     public String getDiscriminatingValue(ILoggingEvent event) {
76
77         Level level = event.getLevel();
78         final int levelInt = level == null ? DEFAULT_LEVEL : level.toInt();
79         if ((levelInt > MIN_ERROR_LEVEL - 1) && (levelInt < MAX_ERROR_LEVEL + 1)) {
80             return ERROR;
81         }
82
83         if (levelInt == Level.DEBUG_INT) {
84             return DEBUG;
85         }
86
87         /*
88          * After DEBUG, ERROR, and WARNING have been filtered out,
89          * only TRACE and INFO are left. TRACE is less than DEBUG
90          * and therefore cannot be used. So, INFO should be used for
91          * custom routing like AUDIT and METRICS
92          */
93         if (levelInt == Level.INFO_INT) {
94
95             final Marker marker = event.getMarker();
96             if (marker != null) {
97
98                 if (marker.contains(Markers.AUDIT)) {
99                     return AUDIT;
100                 }
101
102                 if (marker.contains(Markers.METRICS)) {
103                     return METRICS;
104                 }
105             }
106
107             return ERROR;
108         }
109
110         return DEFAULT;
111     }
112
113     @Override
114     public String getKey() {
115         return KEY;
116     }
117 }