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 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;
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;
33 public class SLF4JLoggingServiceProvider implements LoggingServiceProvider {
37 REQUEST_ID("RequestId"),
38 SERVICE_NAME("ServiceName"),
39 PARTNER_NAME("PartnerName");
41 private final String key;
43 ContextField(String key) {
53 public Logger getLogger(String className) {
54 Objects.requireNonNull(className, "Name cannot be null");
55 return new SLF4JLoggerWrapper(className);
59 public Logger getLogger(Class<?> clazz) {
60 Objects.requireNonNull(clazz, "Class cannot be null");
61 return new SLF4JLoggerWrapper(clazz);
65 public void putRequestId(String requestId) {
66 put(REQUEST_ID.key, requestId);
70 public void putServiceName(String serviceName) {
71 put(SERVICE_NAME.key, serviceName);
75 public void putPartnerName(String partnerName) {
76 put(PARTNER_NAME.key, partnerName);
81 for (ContextField s : ContextField.values()) {
86 private void put(String key, String value) {
87 MDC.put(key, Objects.requireNonNull(value, key));
91 public Runnable copyToRunnable(Runnable runnable) {
92 Objects.requireNonNull(runnable, "Runnable cannot be null");
93 return new MDCRunnableWrapper(runnable);
97 public <V> Callable<V> copyToCallable(Callable<V> callable) {
98 Objects.requireNonNull(callable, "Runnable cannot be null");
99 return new MDCCallableWrapper<>(callable);
103 public String toString() {
104 return this.getClass().getName();