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