[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-logging-lib / openecomp-sdc-logging-core / src / main / java / org / openecomp / sdc / logging / logback / EventTypeDiscriminator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.logging.logback;
22
23 import ch.qos.logback.classic.Level;
24 import ch.qos.logback.classic.spi.ILoggingEvent;
25 import ch.qos.logback.core.sift.AbstractDiscriminator;
26 import org.openecomp.sdc.logging.Markers;
27 import org.slf4j.Marker;
28
29 /**
30  * Can be used with {@link ch.qos.logback.classic.sift.SiftingAppender} to route events of different types to
31  * separate log files. For example,
32  *
33  * <pre>
34  *     &lt;configuration&gt;
35  *         &lt;appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"&gt;
36  *             &lt;discriminator class="org.openecomp.sdc.logging.logback.EventTypeDiscriminator"/&gt;
37  *             &lt;sift&gt;
38  *                  &lt;appender name="{EventType}" class="ch.qos.logback.core.rolling.RollingFileAppender"&gt;
39  *                      &lt;file&gt;${logDirectory}/${eventType}.log&lt;/file&gt;
40  *                      &lt;rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"&gt;
41  *                          &lt;fileNamePattern&gt;${logDirectory}/${eventType}.%i.log.zip&lt;/fileNamePattern&gt;
42  *                          &lt;minIndex&gt;1&lt;/minIndex&gt;
43  *                          &lt;maxIndex&gt;9&lt;/maxIndex&gt;
44  *                      &lt;/rollingPolicy&gt;
45  *                      &lt;triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"&gt;
46  *                          &lt;maxFileSize&gt;5MB&lt;/maxFileSize&gt;
47  *                      &lt;/triggeringPolicy&gt;
48  *                      &lt;encoder&gt;
49  *                          &lt;pattern&gt;${defaultPattern}&lt;/pattern&gt;
50  *                      &lt;/encoder&gt;
51  *                  &lt;/appender&gt;
52  *             &lt;/sift&gt;
53  *         &lt;/appender&gt;
54  *
55  *         &lt;root level="INFO"&gt;
56  *             &lt;appender-ref ref="SIFT" /&gt;
57  *         &lt;/root&gt;
58  *     &lt;/configuration&gt;
59  * </pre>
60  *
61  * @author evitaliy
62  * @since 21/07/2016.
63  */
64 public class EventTypeDiscriminator extends AbstractDiscriminator<ILoggingEvent> {
65
66     private static final String KEY = "eventType";
67
68     private static final String AUDIT = "Audit";
69     private static final String METRICS = "Metrics";
70     private static final String ERROR = "Error";
71     private static final String DEBUG = "Debug";
72     private static final String DEFAULT = DEBUG;
73
74     private static final int MIN_ERROR_LEVEL = Level.WARN_INT;
75     private static final int MAX_ERROR_LEVEL = Level.ERROR_INT;
76     private static final int DEFAULT_LEVEL = Level.DEBUG_INT;
77
78     @Override
79     public String getDiscriminatingValue(ILoggingEvent event) {
80
81         Level level = event.getLevel();
82         final int levelInt = level == null ? DEFAULT_LEVEL : level.toInt();
83         if ((levelInt > MIN_ERROR_LEVEL - 1) && (levelInt < MAX_ERROR_LEVEL + 1)) {
84             return ERROR;
85         }
86
87         if (levelInt == Level.DEBUG_INT) {
88             return DEBUG;
89         }
90
91         /*
92          * After DEBUG, ERROR, and WARNING have been filtered out,
93          * only TRACE and INFO are left. TRACE is less than DEBUG
94          * and therefore cannot be used. So, INFO should be used for
95          * custom routing like AUDIT and METRICS
96          */
97         if (levelInt == Level.INFO_INT) {
98
99             final Marker marker = event.getMarker();
100             if (marker != null) {
101
102                 if (marker.contains(Markers.AUDIT)) {
103                     return AUDIT;
104                 }
105
106                 if (marker.contains(Markers.METRICS)) {
107                     return METRICS;
108                 }
109             }
110
111             return ERROR;
112         }
113
114         return DEFAULT;
115     }
116
117     @Override
118     public String getKey() {
119         return KEY;
120     }
121 }