2 * Copyright © 2016-2018 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.slf4j;
19 import java.util.EnumMap;
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.
32 private MDCDelegate() {
33 // static methods only, prevent instantiation
37 * Get a copy of logging MDC fields.
39 static Map<ContextField, String> copy() {
41 Map<ContextField, String> copy = new EnumMap<>(ContextField.class);
42 for (ContextField k : ContextField.values()) {
43 String v = MDC.get(k.asKey());
53 * Entirely replaces the logging MDC context with the content of the argument. Logging keys that are not present in
54 * the input map will be cleared from MDC.
56 static void replace(Map<ContextField, String> values) {
58 for (ContextField key : ContextField.values()) {
59 updateKey(key, values.get(key));
64 * Push data by multiple data providers on MDC.
66 static void put(ContextProvider... dataProviders) {
70 for (ContextProvider provider : dataProviders) {
71 push(provider.values());
76 * Updates the logging MDC context with the content of the argument. Logging keys that are not present in the input
77 * map will remain "as is", keys with null values will be cleared from MDC.
79 private static void push(Map<ContextField, String> values) {
81 for (Map.Entry<ContextField, String> entry : values.entrySet()) {
82 updateKey(entry.getKey(), entry.getValue());
86 private static void updateKey(ContextField key, String value) {
89 MDC.put(key.asKey(), value);
91 MDC.remove(key.asKey());
97 for (ContextField field : ContextField.values()) {
98 MDC.remove(field.asKey());