6f69aae1b63ba9608442cac5774de44a53884876
[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 static org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField.PARTNER_NAME;
20 import static org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField.REQUEST_ID;
21 import static org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField.SERVICE_NAME;
22
23 import java.util.Objects;
24 import java.util.concurrent.Callable;
25 import org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.spi.LoggingServiceProvider;
27 import org.slf4j.MDC;
28
29 /**
30  * @author evitaliy
31  * @since 13 Sep 2016
32  */
33 public class SLF4JLoggingServiceProvider implements LoggingServiceProvider {
34
35     enum ContextField {
36
37         REQUEST_ID("RequestId"),
38         SERVICE_NAME("ServiceName"),
39         PARTNER_NAME("PartnerName");
40
41         private final String key;
42
43         ContextField(String key) {
44             this.key = key;
45         }
46
47         String asKey() {
48             return key;
49         }
50     }
51
52     @Override
53     public Logger getLogger(String className) {
54         Objects.requireNonNull(className, "Name cannot be null");
55         return new SLF4JLoggerWrapper(className);
56     }
57
58     @Override
59     public Logger getLogger(Class<?> clazz) {
60         Objects.requireNonNull(clazz, "Class cannot be null");
61         return new SLF4JLoggerWrapper(clazz);
62     }
63
64     @Override
65     public void putRequestId(String requestId) {
66         put(REQUEST_ID.key, requestId);
67     }
68
69     @Override
70     public void putServiceName(String serviceName) {
71         put(SERVICE_NAME.key, serviceName);
72     }
73
74     @Override
75     public void putPartnerName(String partnerName) {
76         put(PARTNER_NAME.key, partnerName);
77     }
78
79     @Override
80     public void clear() {
81         for (ContextField s : ContextField.values()) {
82             MDC.remove(s.key);
83         }
84     }
85
86     private void put(String key, String value) {
87         MDC.put(key, Objects.requireNonNull(value, key));
88     }
89
90     @Override
91     public Runnable copyToRunnable(Runnable runnable) {
92         Objects.requireNonNull(runnable, "Runnable cannot be null");
93         return new MDCRunnableWrapper(runnable);
94     }
95
96     @Override
97     public <V> Callable<V> copyToCallable(Callable<V> callable) {
98         Objects.requireNonNull(callable, "Runnable cannot be null");
99         return new MDCCallableWrapper<>(callable);
100     }
101
102     @Override
103     public String toString() {
104         return this.getClass().getName();
105     }
106 }