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.core.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.core.logging.Markers;
27 import org.slf4j.Marker;
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,
33 * <configuration>
34 * <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
35 * <discriminator class="EventTypeDiscriminator"/>
37 * <appender name="{EventType}"
38 * class="ch.qos.logback.core.rolling.RollingFileAppender">
39 * <file>${logDirectory}/${eventType}.log</file>
40 * <rollingPolicy class="ch.
41 * qos.logback.core.rolling.FixedWindowRollingPolicy">
42 * <fileNamePattern>
43 * ${logDirectory}/${eventType}.
44 * %i.log.zip</fileNamePattern>
45 * <minIndex>1</minIndex>
46 * <maxIndex>9</maxIndex>
47 * </rollingPolicy>
48 * <triggeringPolicy
49 * class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
50 * <maxFileSize>5MB</maxFileSize>
51 * </triggeringPolicy>
53 * <pattern>${defaultPattern}</pattern>
58 * <root level="INFO">
59 * <appender-ref ref="SIFT" />
61 * </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() {