diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core')
17 files changed, 62 insertions, 146 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java index 6e63728a07..b27a0c857e 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.context; import java.net.InetAddress; @@ -25,10 +24,10 @@ import java.util.Optional; import java.util.function.Supplier; /** - * Holds a reference to local host address as returned by Java runtime. A value of host address will be cached for the - * interval specified in the constructor or {@link #DEFAULT_REFRESH_INTERVAL}. The caching helps to avoid many low-level - * calls, but at the same time pick up any IP or FQDN changes. Although the underlying JDK implementation uses caching - * too, the refresh interval for logging may be much longer due to the nature of the use. + * Holds a reference to local host address as returned by Java runtime. A value of host address will be cached for the interval specified in the + * constructor or {@link #DEFAULT_REFRESH_INTERVAL}. The caching helps to avoid many low-level calls, but at the same time pick up any IP or FQDN + * changes. Although the underlying JDK implementation uses caching too, the refresh interval for logging may be much longer due to the nature of the + * use. * * @author evitaliy * @since 26 Mar 2018 @@ -37,12 +36,9 @@ import java.util.function.Supplier; public class HostAddressCache { private static final long DEFAULT_REFRESH_INTERVAL = 60000L; // 1 min - private final long interval; - - private volatile CacheEntry cachedAddress; - private final Supplier<InetAddress> readAddress; + private volatile CacheEntry cachedAddress; public HostAddressCache() { this(DEFAULT_REFRESH_INTERVAL); @@ -69,54 +65,27 @@ public class HostAddressCache { this.cachedAddress = new CacheEntry(System.currentTimeMillis(), this.readAddress.get()); } - /** - * Returns an address (host name and IP address) of the local system. - * - * @return local host address or <code>null</code> if it could not be read for some reason - */ - public synchronized Optional<InetAddress> get() { - - long current = System.currentTimeMillis(); - if (current - cachedAddress.lastUpdated < interval) { - return Optional.ofNullable(cachedAddress.address); - } - - InetAddress address = readAddress.get(); // register the attempt even if null, i.e. failed to get a meaningful address - cachedAddress = new CacheEntry(current, address); - return Optional.ofNullable(address); - } - private static InetAddress read() { - try { return InetAddress.getLocalHost(); } catch (UnknownHostException e) { - System.err.println( - "[WARNING] Failed to get local host address. Using a fallback. If you are on Linux, make sure " - + "/etc/hosts contains the host name of your machine, " - + "e.g. '127.0.0.1 localhost my-host.example.com'."); - + System.err.println("[WARNING] Failed to get local host address. Using a fallback. If you are on Linux, make sure " + + "/etc/hosts contains the host name of your machine, " + "e.g. '127.0.0.1 localhost my-host.example.com'."); e.printStackTrace(); // can't really use logging return getFallbackLocalHost(); } } private static InetAddress getFallbackLocalHost() { - try { - Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); - while (networkInterfaces.hasMoreElements()) { - InetAddress address = getAddress(networkInterfaces.nextElement()); if (address != null) { return address; } } - return null; - } catch (SocketException e) { e.printStackTrace(); // can't really use logging return null; @@ -124,26 +93,36 @@ public class HostAddressCache { } private static InetAddress getAddress(NetworkInterface networkInterface) throws SocketException { - if (networkInterface.isLoopback() || networkInterface.isUp()) { return null; } - Enumeration<InetAddress> interfaceAddresses = networkInterface.getInetAddresses(); while (interfaceAddresses.hasMoreElements()) { - InetAddress address = interfaceAddresses.nextElement(); if (isHostAddress(address)) { return address; } } - return null; } private static boolean isHostAddress(InetAddress address) { - return !address.isLoopbackAddress() && !address.isAnyLocalAddress() && !address.isLinkLocalAddress() - && !address.isMulticastAddress(); + return !address.isLoopbackAddress() && !address.isAnyLocalAddress() && !address.isLinkLocalAddress() && !address.isMulticastAddress(); + } + + /** + * Returns an address (host name and IP address) of the local system. + * + * @return local host address or <code>null</code> if it could not be read for some reason + */ + public synchronized Optional<InetAddress> get() { + long current = System.currentTimeMillis(); + if (current - cachedAddress.lastUpdated < interval) { + return Optional.ofNullable(cachedAddress.address); + } + InetAddress address = readAddress.get(); // register the attempt even if null, i.e. failed to get a meaningful address + cachedAddress = new CacheEntry(current, address); + return Optional.ofNullable(address); } private static class CacheEntry { diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/InstanceId.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/InstanceId.java index 73544e1d9f..354aaa24df 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/InstanceId.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/InstanceId.java @@ -13,14 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.context; import java.util.UUID; /** - * Holds a unique ID of the logging entity. Is useful to distinguish between different nodes of the same application. If - * it can be assumed, that the node can be re-started, then the unique ID must be retained on the disk. + * Holds a unique ID of the logging entity. Is useful to distinguish between different nodes of the same application. If it can be assumed, that the + * node can be re-started, then the unique ID must be retained on the disk. * * @author evitaliy * @since 04 Mar 2018 @@ -32,7 +31,9 @@ public class InstanceId { static { // for some reason Java Preferences API + // https://docs.oracle.com/javase/8/docs/technotes/guides/preferences/overview.html + // didn't work in a Docker container, so for now just generate an ID every time INSTANCE_ID = UUID.randomUUID().toString(); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/logback/EventTypeDiscriminator.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/logback/EventTypeDiscriminator.java index aa4657f83e..15471258b3 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/logback/EventTypeDiscriminator.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/logback/EventTypeDiscriminator.java @@ -4,16 +4,15 @@ * 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.logback; import ch.qos.logback.classic.Level; @@ -23,8 +22,7 @@ import org.openecomp.sdc.logging.slf4j.Markers; import org.slf4j.Marker; /** - * Can be used with {@link ch.qos.logback.classic.sift.SiftingAppender} to route events of different types to - * separate log files. For example, + * Can be used with {@link ch.qos.logback.classic.sift.SiftingAppender} to route events of different types to separate log files. For example, * * <pre> * <configuration> @@ -60,30 +58,25 @@ import org.slf4j.Marker; public class EventTypeDiscriminator extends AbstractDiscriminator<ILoggingEvent> { private static final String KEY = "eventType"; - private static final String AUDIT = "Audit"; private static final String METRICS = "Metrics"; private static final String ERROR = "Error"; private static final String DEBUG = "Debug"; private static final String DEFAULT = DEBUG; - private static final int MIN_ERROR_LEVEL = Level.WARN_INT; private static final int MAX_ERROR_LEVEL = Level.ERROR_INT; private static final int DEFAULT_LEVEL = Level.DEBUG_INT; @Override public String getDiscriminatingValue(ILoggingEvent event) { - Level level = event.getLevel(); final int levelInt = level == null ? DEFAULT_LEVEL : level.toInt(); if ((levelInt > MIN_ERROR_LEVEL - 1) && (levelInt < MAX_ERROR_LEVEL + 1)) { return ERROR; } - if (levelInt == Level.DEBUG_INT) { return DEBUG; } - /* * After DEBUG, ERROR, and WARNING have been filtered out, * only TRACE and INFO are left. TRACE is less than DEBUG @@ -91,22 +84,17 @@ public class EventTypeDiscriminator extends AbstractDiscriminator<ILoggingEvent> * custom routing like AUDIT and METRICS */ if (levelInt == Level.INFO_INT) { - final Marker marker = event.getMarker(); if (marker != null) { - - if (marker.contains(Markers.ENTRY) || marker.contains(Markers.EXIT)){ + if (marker.contains(Markers.ENTRY) || marker.contains(Markers.EXIT)) { return AUDIT; } - if (marker.contains(Markers.METRICS)) { return METRICS; } } - return ERROR; } - return DEFAULT; } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/AuditField.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/AuditField.java index c08952a84d..26cb095fd5 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/AuditField.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/AuditField.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import org.onap.logging.ref.slf4j.ONAPLogConstants; @@ -25,7 +24,7 @@ import org.onap.logging.ref.slf4j.ONAPLogConstants; * @since 25 Mar 2018 */ enum AuditField implements MDCField { - + // @formatter:off BEGIN_TIMESTAMP(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP), END_TIMESTAMP("EndTimestamp"), ELAPSED_TIME("ElapsedTime"), @@ -34,6 +33,7 @@ enum AuditField implements MDCField { RESPONSE_DESCRIPTION(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION), CLIENT_IP_ADDRESS(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS), INVOCATION_ID(ONAPLogConstants.MDCs.INVOCATION_ID); + // @formatter:on private final String key; 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 index 25784fefcd..c086d6d47d 100644 --- 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 @@ -13,14 +13,13 @@ * 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. + * 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 @@ -34,8 +33,8 @@ final class Context { } /** - * 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. + * 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 */ 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 index 87aa0e3a32..619426b883 100644 --- 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import org.onap.logging.ref.slf4j.ONAPLogConstants; @@ -25,13 +24,14 @@ import org.onap.logging.ref.slf4j.ONAPLogConstants; * @since 23 Mar 2018 */ enum ContextField implements MDCField { - + // @formatter:off REQUEST_ID(ONAPLogConstants.MDCs.REQUEST_ID), SERVICE_NAME(ONAPLogConstants.MDCs.SERVICE_NAME), PARTNER_NAME(ONAPLogConstants.MDCs.PARTNER_NAME), INSTANCE_ID(ONAPLogConstants.MDCs.INSTANCE_UUID), SERVER(ONAPLogConstants.MDCs.SERVER_FQDN), SERVER_IP_ADDRESS("ServerIpAddress"); + // @formatter:on private final String 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 index f9a7144b22..e62ceb6f91 100644 --- 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import java.util.Map; @@ -25,5 +24,6 @@ import java.util.Map; * @since 23 Mar 2018 */ interface ContextProvider { + Map<ContextField, String> 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 index f6e933d57b..109671bf25 100644 --- 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import java.net.InetAddress; @@ -35,16 +34,13 @@ class GlobalContextProvider implements ContextProvider { @Override public Map<ContextField, String> values() { - Map<ContextField, String> values = new EnumMap<>(ContextField.class); values.put(ContextField.INSTANCE_ID, InstanceId.get()); - Optional<InetAddress> hostAddress = HOST_ADDRESS_CACHE.get(); hostAddress.ifPresent(address -> { values.put(ContextField.SERVER, address.getHostName()); values.put(ContextField.SERVER_IP_ADDRESS, address.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 84aa256d95..bf10fc2598 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import java.util.Map; @@ -28,7 +27,6 @@ import java.util.concurrent.Callable; class MDCCallableWrapper<V> implements Callable<V> { private final Context context = new Context(); - private final Callable<V> task; MDCCallableWrapper(Callable<V> task) { @@ -37,9 +35,7 @@ class MDCCallableWrapper<V> implements Callable<V> { @Override public V call() throws Exception { - Map<ContextField, String> oldContext = context.replace(); - try { return task.call(); } finally { 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 index 026d3fa99b..23544f844b 100644 --- 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import java.util.EnumMap; @@ -21,8 +20,7 @@ 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. + * 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 @@ -37,7 +35,6 @@ class MDCDelegate { * Get a copy of logging MDC fields. */ static Map<ContextField, String> copy() { - Map<ContextField, String> copy = new EnumMap<>(ContextField.class); for (ContextField k : ContextField.values()) { String v = MDC.get(k.asKey()); @@ -45,7 +42,6 @@ class MDCDelegate { copy.put(k, v); } } - return copy; } @@ -60,22 +56,18 @@ class MDCDelegate { * Reads selected fields from MDC. */ static Map<ContextField, String> get(ContextField... fields) { - Map<ContextField, String> values = new EnumMap<>(ContextField.class); - for (ContextField key : fields) { values.put(key, MDC.get(key.asKey())); } - return values; } /** - * 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. + * 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<ContextField, String> values) { - for (ContextField key : ContextField.values()) { updateKey(key, values.get(key)); } @@ -85,27 +77,23 @@ class MDCDelegate { * 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. + * 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<ContextField, String> values) { - for (Map.Entry<ContextField, String> 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 { @@ -114,7 +102,6 @@ class MDCDelegate { } 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/MDCField.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCField.java index 1096face85..518f911337 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCField.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MDCField.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; /** @@ -25,5 +24,4 @@ package org.openecomp.sdc.logging.slf4j; interface MDCField { String 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 9d93246181..dba96af7af 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import java.util.Map; @@ -27,7 +26,6 @@ import java.util.Map; class MDCRunnableWrapper implements Runnable { private final Context context = new Context(); - private final Runnable task; MDCRunnableWrapper(Runnable task) { @@ -36,9 +34,7 @@ class MDCRunnableWrapper implements Runnable { @Override public void run() { - Map<ContextField, String> oldContext = context.replace(); - try { task.run(); } finally { diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Markers.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Markers.java index 3d9bbcfa53..2225f60a1a 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Markers.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/Markers.java @@ -4,16 +4,15 @@ * 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 org.onap.logging.ref.slf4j.ONAPLogConstants; @@ -36,15 +35,13 @@ import org.slf4j.MarkerFactory; * </pre> * * @author EVITALIY - * @since 13/09/2016. - * * @see Marker + * @since 13/09/2016. */ public class Markers { public static final Marker ENTRY = MarkerFactory.getMarker(ONAPLogConstants.Markers.ENTRY.getName()); public static final Marker EXIT = MarkerFactory.getMarker(ONAPLogConstants.Markers.EXIT.getName()); - public static final Marker METRICS = MarkerFactory.getMarker("METRICS"); private Markers() { diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MetricsField.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MetricsField.java index c4e8e143af..2845e71ffc 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MetricsField.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/MetricsField.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import org.onap.logging.ref.slf4j.ONAPLogConstants; @@ -25,7 +24,7 @@ import org.onap.logging.ref.slf4j.ONAPLogConstants; * @since 26 Mar 2018 */ public enum MetricsField implements MDCField { - + // @formatter:off BEGIN_TIMESTAMP(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP), END_TIMESTAMP("EndTimestamp"), ELAPSED_TIME("ElapsedTime"), @@ -35,6 +34,7 @@ public enum MetricsField implements MDCField { CLIENT_IP_ADDRESS(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS), TARGET_VIRTUAL_ENTITY("TargetVirtualEntity"), TARGET_ENTITY(ONAPLogConstants.MDCs.TARGET_ENTITY); + // @formatter:on private final String key; @@ -45,5 +45,4 @@ public enum MetricsField implements MDCField { 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/RequestContextProvider.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/RequestContextProvider.java index 70e3dd6479..38f691f9ed 100644 --- 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import java.util.EnumMap; @@ -39,21 +38,16 @@ class RequestContextProvider implements ContextProvider { } static ContextData to(Map<ContextField, String> values) { - return ContextData.builder() - .requestId(values.get(ContextField.REQUEST_ID)) - .serviceName(values.get(ContextField.SERVICE_NAME)) - .partnerName(values.get(ContextField.PARTNER_NAME)).build(); + return ContextData.builder().requestId(values.get(ContextField.REQUEST_ID)).serviceName(values.get(ContextField.SERVICE_NAME)) + .partnerName(values.get(ContextField.PARTNER_NAME)).build(); } @Override public Map<ContextField, String> values() { - Map<ContextField, String> 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; } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java index 54026098c1..80ca807f2b 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import java.text.SimpleDateFormat; @@ -34,7 +33,6 @@ class SLF4JLoggerWrapper implements Logger { //The specified format presents time in UTC formatted per ISO 8601, as required by ONAP logging guidelines private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; - private final org.slf4j.Logger logger; // May cause http://www.slf4j.org/codes.html#loggerNameMismatch @@ -50,6 +48,16 @@ class SLF4JLoggerWrapper implements Logger { this(LoggerFactory.getLogger(className)); } + private static void unsafePutOnMdc(MDCField field, String value) { + MDC.put(field.asKey(), value); + } + + private static void safePutOnMdc(MDCField field, String value) { + if (value != null) { + unsafePutOnMdc(field, value); + } + } + @Override public String getName() { return logger.getName(); @@ -67,11 +75,9 @@ class SLF4JLoggerWrapper implements Logger { @Override public void metrics(MetricsData data) { - if (data == null) { return; // not going to fail because of null } - try { putMetricsOnMdc(data); logger.info(Markers.METRICS, ""); @@ -81,7 +87,6 @@ class SLF4JLoggerWrapper implements Logger { } private void putMetricsOnMdc(MetricsData metrics) { - SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_PATTERN); unsafePutOnMdc(MetricsField.BEGIN_TIMESTAMP, dateFormat.format(metrics.getStartTime())); unsafePutOnMdc(MetricsField.END_TIMESTAMP, dateFormat.format(metrics.getEndTime())); @@ -91,7 +96,6 @@ class SLF4JLoggerWrapper implements Logger { safePutOnMdc(MetricsField.CLIENT_IP_ADDRESS, metrics.getClientIpAddress()); safePutOnMdc(MetricsField.TARGET_ENTITY, metrics.getTargetEntity()); safePutOnMdc(MetricsField.TARGET_VIRTUAL_ENTITY, metrics.getTargetVirtualEntity()); - if (metrics.getStatusCode() != null) { unsafePutOnMdc(MetricsField.STATUS_CODE, metrics.getStatusCode().name()); } @@ -103,16 +107,6 @@ class SLF4JLoggerWrapper implements Logger { } } - private static void unsafePutOnMdc(MDCField field, String value) { - MDC.put(field.asKey(), value); - } - - private static void safePutOnMdc(MDCField field, String value) { - if (value != null) { - unsafePutOnMdc(field, value); - } - } - @Override public boolean isAuditEnabled() { return logger.isInfoEnabled(Markers.EXIT); @@ -120,11 +114,9 @@ class SLF4JLoggerWrapper implements Logger { @Override public void auditEntry(AuditData data) { - if (data == null) { return; // not failing if null } - try { putAuditOnMdc(data); logger.info(Markers.ENTRY, ""); @@ -133,14 +125,11 @@ class SLF4JLoggerWrapper implements Logger { } } - @Override public void auditExit(AuditData data) { - if (data == null) { return; // not failing if null } - try { putAuditOnMdc(data); logger.info(Markers.EXIT, ""); @@ -150,7 +139,6 @@ class SLF4JLoggerWrapper implements Logger { } private void putAuditOnMdc(AuditData audit) { - SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_PATTERN); unsafePutOnMdc(AuditField.BEGIN_TIMESTAMP, dateFormat.format(audit.getStartTime())); unsafePutOnMdc(AuditField.END_TIMESTAMP, dateFormat.format(audit.getEndTime())); @@ -159,7 +147,6 @@ class SLF4JLoggerWrapper implements Logger { safePutOnMdc(AuditField.RESPONSE_DESCRIPTION, audit.getResponseDescription()); safePutOnMdc(AuditField.CLIENT_IP_ADDRESS, audit.getClientIpAddress()); unsafePutOnMdc(AuditField.INVOCATION_ID, UUID.randomUUID().toString()); - if (audit.getStatusCode() != null) { unsafePutOnMdc(AuditField.STATUS_CODE, audit.getStatusCode().name()); } 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 9d4d507ac5..a74f606bc0 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 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.logging.slf4j; import java.util.Objects; |