2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.logging.logback;
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;
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,
34 * <configuration>
35 * <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
36 * <discriminator class="org.openecomp.sdc.logging.logback.EventTypeDiscriminator"/>
38 * <appender name="{EventType}" class="ch.qos.logback.core.rolling.RollingFileAppender">
39 * <file>${logDirectory}/${eventType}.log</file>
40 * <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
41 * <fileNamePattern>${logDirectory}/${eventType}.%i.log.zip</fileNamePattern>
42 * <minIndex>1</minIndex>
43 * <maxIndex>9</maxIndex>
44 * </rollingPolicy>
45 * <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
46 * <maxFileSize>5MB</maxFileSize>
47 * </triggeringPolicy>
49 * <pattern>${defaultPattern}</pattern>
55 * <root level="INFO">
56 * <appender-ref ref="SIFT" />
58 * </configuration>
64 public class EventTypeDiscriminator extends AbstractDiscriminator<ILoggingEvent> {
66 private static final String KEY = "eventType";
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;
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;
79 public String getDiscriminatingValue(ILoggingEvent event) {
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)) {
87 if (levelInt == Level.DEBUG_INT) {
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
97 if (levelInt == Level.INFO_INT) {
99 final Marker marker = event.getMarker();
100 if (marker != null) {
102 if (marker.contains(Markers.AUDIT)) {
106 if (marker.contains(Markers.METRICS)) {
118 public String getKey() {