From 6eceb46537df31756446760484ad1925a997205a Mon Sep 17 00:00:00 2001 From: vempo Date: Fri, 23 Mar 2018 23:10:30 +0300 Subject: Logging refactoring and global context Implemented global context (instance ID, host address). Refactored the logging context API for clarity and separation of concerns. Added unit tests. Fixed Javadoc. Change-Id: I6e29b7b3613aebf23112dc017490ad2828e0fb91 Issue-ID: SDC-772 Signed-off-by: vempo --- .../sdc/logging/GlobalLoggingContext.java | 82 ++--------------- .../sdc/logging/slf4j/BaseMDCCopyingWrapper.java | 73 --------------- .../org/openecomp/sdc/logging/slf4j/Context.java | 56 ++++++++++++ .../openecomp/sdc/logging/slf4j/ContextField.java | 43 +++++++++ .../sdc/logging/slf4j/ContextProvider.java | 29 ++++++ .../sdc/logging/slf4j/GlobalContextProvider.java | 46 ++++++++++ .../sdc/logging/slf4j/MDCCallableWrapper.java | 14 +-- .../openecomp/sdc/logging/slf4j/MDCDelegate.java | 101 +++++++++++++++++++++ .../sdc/logging/slf4j/MDCRunnableWrapper.java | 14 +-- .../sdc/logging/slf4j/RequestContextProvider.java | 55 +++++++++++ .../logging/slf4j/SLF4JLoggingServiceProvider.java | 48 ++-------- 11 files changed, 363 insertions(+), 198 deletions(-) delete mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/BaseMDCCopyingWrapper.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Context.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/ContextField.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/ContextProvider.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCDelegate.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/RequestContextProvider.java (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main') diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/GlobalLoggingContext.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/GlobalLoggingContext.java index 95dc52c85c..a708ed6715 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/GlobalLoggingContext.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/GlobalLoggingContext.java @@ -16,13 +16,8 @@ package org.openecomp.sdc.logging; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.net.InetAddress; import java.net.UnknownHostException; -import java.nio.charset.StandardCharsets; -import java.util.Properties; import java.util.UUID; import java.util.concurrent.atomic.AtomicLong; import java.util.prefs.BackingStoreException; @@ -38,45 +33,20 @@ import java.util.prefs.Preferences; @SuppressWarnings({"UseOfSystemOutOrSystemErr", "CallToPrintStackTrace", "squid:S106", "squid:S1148"}) public class GlobalLoggingContext { - private static final String APPLICATION_ID_KEY = "ApplicationId"; - - private static final String CONFIGURATION_RESOURCE = "META-INF/logging/logger.properties"; - - @SuppressWarnings("squid:S1075") - private static final String ID_PREFERENCES_PATH = "/logging/instance/uuid"; - - private static final String APP_DISTINGUISHER_KEY = "app.distinguisher"; - // should be cashed to avoid low-level call, but with a timeout to account for IP or FQDN changes - private static final HostAddressCache HOST_ADDRESS = new HostAddressCache(); - - private static final String DISTINGUISHER; + private static final HostAddressCache HOST_ADDRESS_CACHE = new HostAddressCache(); - private static final String APPLICATION_ID; + @SuppressWarnings("squid:S1075") + private static final String INSTANCE_UUID_PREFERENCES_PATH = "/logging/instance/uuid"; private static final String INSTANCE_ID; static { - APPLICATION_ID = System.getProperty(APPLICATION_ID_KEY); - DISTINGUISHER = readDistinguisher(); INSTANCE_ID = readInstanceId(); } - private GlobalLoggingContext() { /* prevent instantiation */ } - - public static String getApplicationId() { - return APPLICATION_ID; - } - - /** - * A distinguisher to allow separation of logs created by applications running with the same configuration, but - * different class-loaders. For instance, when multiple web application are running in the same container and their - * logger configuration is passed at the JVM level. - * - * @return application distinguisher defined in a properties file - */ - public static String getDistinguisher() { - return DISTINGUISHER; + private GlobalLoggingContext() { + // prevent instantiation } /** @@ -96,25 +66,22 @@ public class GlobalLoggingContext { * @return local host address, may be null if could not be read for some reason */ public static InetAddress getHostAddress() { - return HOST_ADDRESS.get(); + return HOST_ADDRESS_CACHE.get(); } private static String readInstanceId() { - String appId = System.getProperty(APPLICATION_ID_KEY); - String key = ID_PREFERENCES_PATH + (appId == null ? "" : "/" + appId); - try { - // By default, this will be ~/.java/.userPrefs/prefs.xml + // On Linux, by default this will be ~/.java/.userPrefs/prefs.xml final Preferences preferences = Preferences.userRoot(); - String existingId = preferences.get(key, null); + String existingId = preferences.get(INSTANCE_UUID_PREFERENCES_PATH, null); if (existingId != null) { return existingId; } String newId = UUID.randomUUID().toString(); - preferences.put(key, newId); + preferences.put(INSTANCE_UUID_PREFERENCES_PATH, newId); preferences.flush(); return newId; @@ -125,35 +92,6 @@ public class GlobalLoggingContext { } } - private static String readDistinguisher() { - - try { - Properties properties = loadConfiguration(); - return properties.getProperty(APP_DISTINGUISHER_KEY, ""); - } catch (IOException e) { - e.printStackTrace(); // can't write to a log - return ""; - } - } - - private static Properties loadConfiguration() throws IOException { - - Properties properties = new Properties(); - - try (InputStream is = Thread.currentThread().getContextClassLoader() - .getResourceAsStream(CONFIGURATION_RESOURCE)) { - - if (is == null) { - return properties; - } - - try (InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) { - properties.load(reader); - return properties; - } - } - } - private static class HostAddressCache { private static final long REFRESH_TIME = 60000L; @@ -161,7 +99,7 @@ public class GlobalLoggingContext { private final AtomicLong lastUpdated = new AtomicLong(0L); private InetAddress hostAddress; - public InetAddress get() { + InetAddress get() { long current = System.currentTimeMillis(); if (current - lastUpdated.get() > REFRESH_TIME) { diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/BaseMDCCopyingWrapper.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/BaseMDCCopyingWrapper.java deleted file mode 100644 index d667ff769c..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/BaseMDCCopyingWrapper.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright © 2016-2017 European Support Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.openecomp.sdc.logging.slf4j; - -import java.util.EnumMap; -import java.util.Map; -import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; -import org.slf4j.MDC; - -/** - * Because we don't know which information should be carried over from MDC, and which shouldn't, copy just the keys that - * the logging service uses. - * - * @author evitaliy - * @since 08 Jan 2018 - */ -abstract class BaseMDCCopyingWrapper { - - private final Map context; - - BaseMDCCopyingWrapper() { - this.context = fromMdc(); - } - - final Map replace() { - Map old = fromMdc(); - toMdc(this.context); - return old; - } - - final void revert(Map old) { - toMdc(old); - } - - private Map fromMdc() { - - Map copy = new EnumMap<>(ContextField.class); - for (ContextField k : ContextField.values()) { - String v = MDC.get(k.asKey()); - if (v != null) { - copy.put(k, v); - } - } - - return copy; - } - - private static void toMdc(Map context) { - - for (ContextField k : ContextField.values()) { - String v = context.get(k); - if (v != null) { - MDC.put(k.asKey(), v); - } else { - MDC.remove(k.asKey()); - } - } - } -} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Context.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Context.java new file mode 100644 index 0000000000..25784fefcd --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Context.java @@ -0,0 +1,56 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.logging.slf4j; + +import java.util.Map; + +/** + * Does not store a state other than initial context values. Objects of this class may be reused by multiple threads, + * therefore they must be stateless to prevent inadvertent exchange of context values between threads. + * + * @author evitaliy + * @since 08 Jan 2018 + */ +final class Context { + + private final Map originalCtx; + + Context() { + this.originalCtx = MDCDelegate.copy(); + } + + /** + * Pushes the initial context onto current thread, and returns the existing context. The result cannot be stored as + * local state (see the class comments), and must be kept in a local variable to work properly. + * + * @return previous context values + */ + final Map replace() { + Map old = MDCDelegate.copy(); + MDCDelegate.replace(this.originalCtx); + return old; + } + + /** + * Pushes an old context onto current thread. + * + * @param old copy of the old context returned by {@link #replace()} + */ + final void revert(Map old) { + MDCDelegate.replace(old); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/ContextField.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/ContextField.java new file mode 100644 index 0000000000..6aa689bf21 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/ContextField.java @@ -0,0 +1,43 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.logging.slf4j; + +/** + * MDC fields to work with - populate, clear, copy. + * + * @author evitaliy + * @since 23 Mar 2018 + */ +enum ContextField { + + REQUEST_ID("RequestId"), + SERVICE_NAME("ServiceName"), + PARTNER_NAME("PartnerName"), + INSTANCE_ID("InstanceId"), + SERVER("Server"), + SERVER_IP_ADDRESS("ServerIpAddress"); + + private final String key; + + ContextField(String key) { + this.key = key; + } + + public String asKey() { + return key; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/ContextProvider.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/ContextProvider.java new file mode 100644 index 0000000000..f9a7144b22 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/ContextProvider.java @@ -0,0 +1,29 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.logging.slf4j; + +import java.util.Map; + +/** + * Abstracts a source of MDC values. + * + * @author evitaliy + * @since 23 Mar 2018 + */ +interface ContextProvider { + Map values(); +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java new file mode 100644 index 0000000000..a415e22c63 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java @@ -0,0 +1,46 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.logging.slf4j; + +import java.net.InetAddress; +import java.util.EnumMap; +import java.util.Map; +import org.openecomp.sdc.logging.GlobalLoggingContext; + +/** + * Maps global logging context to corresponding MDC fields. + * + * @author evitaliy + * @since 23 Mar 2018 + */ +class GlobalContextProvider implements ContextProvider { + + @Override + public Map values() { + + Map values = new EnumMap<>(ContextField.class); + values.put(ContextField.INSTANCE_ID, GlobalLoggingContext.getInstanceId()); + + InetAddress hostAddress = GlobalLoggingContext.getHostAddress(); + if (hostAddress != null) { + values.put(ContextField.SERVER, hostAddress.getHostName()); + values.put(ContextField.SERVER_IP_ADDRESS, hostAddress.getHostAddress()); + } + + return values; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCCallableWrapper.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCCallableWrapper.java index 07d0f935f7..84aa256d95 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCCallableWrapper.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCCallableWrapper.java @@ -18,30 +18,32 @@ package org.openecomp.sdc.logging.slf4j; import java.util.Map; import java.util.concurrent.Callable; -import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; /** - * @author EVITALIY + * Carries MDC values over to a Callable from the instantiating thread to the moment the callable will run. + * + * @author evitaliy * @since 08 Jan 18 */ -class MDCCallableWrapper extends BaseMDCCopyingWrapper implements Callable { +class MDCCallableWrapper implements Callable { + + private final Context context = new Context(); private final Callable task; MDCCallableWrapper(Callable task) { - super(); this.task = task; } @Override public V call() throws Exception { - Map oldContext = replace(); + Map oldContext = context.replace(); try { return task.call(); } finally { - revert(oldContext); + context.revert(oldContext); } } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCDelegate.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCDelegate.java new file mode 100644 index 0000000000..8d719a2da8 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCDelegate.java @@ -0,0 +1,101 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.logging.slf4j; + +import java.util.EnumMap; +import java.util.Map; +import org.slf4j.MDC; + +/** + * Because we don't know which information should be carried over from MDC, and which shouldn't, copy just the keys that + * the logging service uses. + * + * @author evitaliy + * @since 23 Mar 2018 + */ +class MDCDelegate { + + private MDCDelegate() { + // static methods only, prevent instantiation + } + + /** + * Get a copy of logging MDC fields. + */ + static Map copy() { + + Map copy = new EnumMap<>(ContextField.class); + for (ContextField k : ContextField.values()) { + String v = MDC.get(k.asKey()); + if (v != null) { + copy.put(k, v); + } + } + + return copy; + } + + /** + * Entirely replaces the logging MDC context with the content of the argument. Logging keys that are not present in + * the input map will be cleared from MDC. + */ + static void replace(Map values) { + + for (ContextField key : ContextField.values()) { + updateKey(key, values.get(key)); + } + } + + /** + * Push data by multiple data providers on MDC. + */ + static void put(ContextProvider... dataProviders) { + + clear(); + + for (ContextProvider provider : dataProviders) { + push(provider.values()); + } + } + + /** + * Updates the logging MDC context with the content of the argument. Logging keys that are not present in the input + * map will remain "as is", keys with null values will be cleared from MDC. + */ + private static void push(Map values) { + + for (Map.Entry entry : values.entrySet()) { + updateKey(entry.getKey(), entry.getValue()); + } + } + + private static void updateKey(ContextField key, String value) { + + if (value != null) { + MDC.put(key.asKey(), value); + } else { + MDC.remove(key.asKey()); + } + } + + static void clear() { + + for (ContextField field : ContextField.values()) { + MDC.remove(field.asKey()); + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCRunnableWrapper.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCRunnableWrapper.java index e1b8f1e5c5..9d93246181 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCRunnableWrapper.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCRunnableWrapper.java @@ -17,30 +17,32 @@ package org.openecomp.sdc.logging.slf4j; import java.util.Map; -import org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField; /** - * @author EVITALIY + * Carries MDC values over to a Runnable from the instantiating thread to the moment the callable will run. + * + * @author evitaliy * @since 08 Jan 18 */ -class MDCRunnableWrapper extends BaseMDCCopyingWrapper implements Runnable { +class MDCRunnableWrapper implements Runnable { + + private final Context context = new Context(); private final Runnable task; MDCRunnableWrapper(Runnable task) { - super(); this.task = task; } @Override public void run() { - Map oldContext = replace(); + Map oldContext = context.replace(); try { task.run(); } finally { - revert(oldContext); + context.revert(oldContext); } } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/RequestContextProvider.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/RequestContextProvider.java new file mode 100644 index 0000000000..d79771c1c5 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/RequestContextProvider.java @@ -0,0 +1,55 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.logging.slf4j; + +import java.util.EnumMap; +import java.util.Map; +import org.openecomp.sdc.logging.api.ContextData; + +/** + * Maps request data sent to the context service to corresponding MDC fields. + * + * @author evitaliy + * @since 23 Mar 2018 + */ +class RequestContextProvider implements ContextProvider { + + private final ContextData data; + + RequestContextProvider(ContextData contextData) { + this.data = contextData; + } + + @Override + public Map values() { + + Map values = new EnumMap<>(ContextField.class); + + putIfNotNull(values, ContextField.REQUEST_ID, data.getRequestId()); + putIfNotNull(values, ContextField.SERVICE_NAME, data.getServiceName()); + putIfNotNull(values, ContextField.PARTNER_NAME, data.getPartnerName()); + + return values; + } + + private void putIfNotNull(Map values, ContextField field, String value) { + + if (value != null) { + values.put(field, value); + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java index 6f69aae1b6..1a5d6facc2 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java @@ -16,39 +16,20 @@ package org.openecomp.sdc.logging.slf4j; -import static org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField.PARTNER_NAME; -import static org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField.REQUEST_ID; -import static org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.ContextField.SERVICE_NAME; - import java.util.Objects; import java.util.concurrent.Callable; +import org.openecomp.sdc.logging.api.ContextData; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.spi.LoggingServiceProvider; -import org.slf4j.MDC; /** + * Uses SLF4J as backend for logging service. + * * @author evitaliy * @since 13 Sep 2016 */ public class SLF4JLoggingServiceProvider implements LoggingServiceProvider { - enum ContextField { - - REQUEST_ID("RequestId"), - SERVICE_NAME("ServiceName"), - PARTNER_NAME("PartnerName"); - - private final String key; - - ContextField(String key) { - this.key = key; - } - - String asKey() { - return key; - } - } - @Override public Logger getLogger(String className) { Objects.requireNonNull(className, "Name cannot be null"); @@ -62,29 +43,14 @@ public class SLF4JLoggingServiceProvider implements LoggingServiceProvider { } @Override - public void putRequestId(String requestId) { - put(REQUEST_ID.key, requestId); - } - - @Override - public void putServiceName(String serviceName) { - put(SERVICE_NAME.key, serviceName); - } - - @Override - public void putPartnerName(String partnerName) { - put(PARTNER_NAME.key, partnerName); + public void put(ContextData contextData) { + Objects.requireNonNull(contextData, "Context data cannot be null"); + MDCDelegate.put(new RequestContextProvider(contextData), new GlobalContextProvider()); } @Override public void clear() { - for (ContextField s : ContextField.values()) { - MDC.remove(s.key); - } - } - - private void put(String key, String value) { - MDC.put(key, Objects.requireNonNull(value, key)); + MDCDelegate.clear(); } @Override -- cgit 1.2.3-korg