2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.logging.logback;
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;
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,
30 * <configuration>
31 * <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
32 * <discriminator class="org.openecomp.sdc.logging.logback.EventTypeDiscriminator"/>
34 * <appender name="{EventType}" class="ch.qos.logback.core.rolling.RollingFileAppender">
35 * <file>${logDirectory}/${eventType}.log</file>
36 * <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
37 * <fileNamePattern>${logDirectory}/${eventType}.%i.log.zip</fileNamePattern>
38 * <minIndex>1</minIndex>
39 * <maxIndex>9</maxIndex>
40 * </rollingPolicy>
41 * <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
42 * <maxFileSize>5MB</maxFileSize>
43 * </triggeringPolicy>
45 * <pattern>${defaultPattern}</pattern>
51 * <root level="INFO">
52 * <appender-ref ref="SIFT" />
54 * </configuration>
60 public class EventTypeDiscriminator extends AbstractDiscriminator<ILoggingEvent> {
62 private static final String KEY = "eventType";
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;
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;
75 public String getDiscriminatingValue(ILoggingEvent event) {
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)) {
83 if (levelInt == Level.DEBUG_INT) {
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
93 if (levelInt == Level.INFO_INT) {
95 final Marker marker = event.getMarker();
98 if (marker.contains(Markers.AUDIT)) {
102 if (marker.contains(Markers.METRICS)) {
114 public String getKey() {