[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / common / openecomp-logging-lib / openecomp-logging-core / src / main / java / org / openecomp / core / 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.core.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.core.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
31  * types to separate log files. For example,
32  * <pre>
33  *     &lt;configuration&gt;
34  *         &lt;appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"&gt;
35  *             &lt;discriminator class="EventTypeDiscriminator"/&gt;
36  *             &lt;sift&gt;
37  *                  &lt;appender name="{EventType}"
38  *                  class="ch.qos.logback.core.rolling.RollingFileAppender"&gt;
39  *                      &lt;file&gt;${logDirectory}/${eventType}.log&lt;/file&gt;
40  *                      &lt;rollingPolicy class="ch.
41  *                      qos.logback.core.rolling.FixedWindowRollingPolicy"&gt;
42  *                          &lt;fileNamePattern&gt;
43  *                          ${logDirectory}/${eventType}.
44  *                          %i.log.zip&lt;/fileNamePattern&gt;
45  *                          &lt;minIndex&gt;1&lt;/minIndex&gt;
46  *                          &lt;maxIndex&gt;9&lt;/maxIndex&gt;
47  *                      &lt;/rollingPolicy&gt;
48  *                      &lt;triggeringPolicy
49  *                      class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"&gt;
50  *                          &lt;maxFileSize&gt;5MB&lt;/maxFileSize&gt;
51  *                      &lt;/triggeringPolicy&gt;
52  *                      &lt;encoder&gt;
53  *                          &lt;pattern&gt;${defaultPattern}&lt;/pattern&gt;
54  *                      &lt;/encoder&gt;
55  *                  &lt;/appender&gt;
56  *             &lt;/sift&gt;
57  *         &lt;/appender&gt;
58  *         &lt;root level="INFO"&gt;
59  *             &lt;appender-ref ref="SIFT" /&gt;
60  *         &lt;/root&gt;
61  *     &lt;/configuration&gt;
62  * </pre>
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 }