re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-logging-lib / openecomp-sdc-logging-core / src / main / java / org / openecomp / sdc / logging / slf4j / MDCDelegate.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.logging.slf4j;
18
19 import org.slf4j.MDC;
20
21 import java.util.EnumMap;
22 import java.util.Map;
23
24 /**
25  * Because we don't know which information should be carried over from MDC, and which shouldn't, copy just the keys that
26  * the logging service uses.
27  *
28  * @author evitaliy
29  * @since 23 Mar 2018
30  */
31 class MDCDelegate {
32
33     private MDCDelegate() {
34         // static methods only, prevent instantiation
35     }
36
37     /**
38      * Get a copy of logging MDC fields.
39      */
40     static Map<ContextField, String> copy() {
41
42         Map<ContextField, String> copy = new EnumMap<>(ContextField.class);
43         for (ContextField k : ContextField.values()) {
44             String v = MDC.get(k.asKey());
45             if (v != null) {
46                 copy.put(k, v);
47             }
48         }
49
50         return copy;
51     }
52
53     /**
54      * Reads all context fields from MDC.
55      */
56     static Map<ContextField, String> get() {
57         return get(ContextField.values());
58     }
59
60     /**
61      * Reads selected fields from MDC.
62      */
63     static Map<ContextField, String> get(ContextField... fields) {
64
65         Map<ContextField, String> values = new EnumMap<>(ContextField.class);
66
67         for (ContextField key : fields) {
68             values.put(key, MDC.get(key.asKey()));
69         }
70
71         return values;
72     }
73
74     /**
75      * Entirely replaces the logging MDC context with the content of the argument. Logging keys that are not present in
76      * the input map will be cleared from MDC.
77      */
78     static void replace(Map<ContextField, String> values) {
79
80         for (ContextField key : ContextField.values()) {
81             updateKey(key, values.get(key));
82         }
83     }
84
85     /**
86      * Push data by multiple data providers on MDC.
87      */
88     static void put(ContextProvider... dataProviders) {
89
90         clear();
91
92         for (ContextProvider provider : dataProviders) {
93             push(provider.values());
94         }
95     }
96
97     /**
98      * Updates the logging MDC context with the content of the argument. Logging keys that are not present in the input
99      * map will remain "as is", keys with null values will be cleared from MDC.
100      */
101     private static void push(Map<ContextField, String> values) {
102
103         for (Map.Entry<ContextField, String> entry : values.entrySet()) {
104             updateKey(entry.getKey(), entry.getValue());
105         }
106     }
107
108     private static void updateKey(ContextField key, String value) {
109
110         if (value != null) {
111             MDC.put(key.asKey(), value);
112         } else {
113             MDC.remove(key.asKey());
114         }
115     }
116
117     static void clear() {
118
119         for (ContextField field : ContextField.values()) {
120             MDC.remove(field.asKey());
121         }
122     }
123 }