From 1cfb08779ea0e00be69e072a940b3063e049fe6b Mon Sep 17 00:00:00 2001 From: Ofir Sonsino Date: Wed, 31 Jan 2018 17:19:00 +0200 Subject: org.onap migration Change-Id: I52f0b2851f2c765752b6d21f49b32136d7d72a3d Issue-ID: VID-86 Signed-off-by: Ofir Sonsino --- .../src/main/java/org/onap/vid/utils/Logging.java | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 vid-app-common/src/main/java/org/onap/vid/utils/Logging.java (limited to 'vid-app-common/src/main/java/org/onap/vid/utils/Logging.java') diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java new file mode 100644 index 00000000..16dde568 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java @@ -0,0 +1,105 @@ +package org.onap.vid.utils; + +import com.att.eelf.configuration.EELFLogger; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.util.SystemProperties; +import org.springframework.http.HttpMethod; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.ProcessingException; +import javax.ws.rs.core.Response; +import java.util.Arrays; +import java.util.Optional; + +import static org.onap.vid.utils.Streams.not; + +public class Logging { + + private Logging() { + } + + public static final String HTTP_REQUESTS_OUTGOING = "http.requests.outgoing."; + + public static final String requestIdHeaderKey = SystemProperties.ECOMP_REQUEST_ID; + + private static ObjectMapper objectMapper = new ObjectMapper(); + + public static String getMethodName() { + return getMethodName(0); + } + + public static String getMethodCallerName() { + return getMethodName(1); + } + + private static String getMethodName(int depth) { + final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); + String thisClassName = stackTrace[1].getClassName(); + final Optional caller = + Arrays.stream(stackTrace) + .skip(1) + .filter(not(frame -> frame.getClassName().equals(thisClassName))) + .skip(depth) + .map(StackTraceElement::getMethodName) + .findFirst(); + return caller.orElse(""); + } + + public static EELFLogger getRequestsLogger(String serverName) { + return EELFLoggerDelegate.getLogger(HTTP_REQUESTS_OUTGOING +serverName); + } + + public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url, final Object body) { + if (!logger.isDebugEnabled()) { + return; + } + + if (body == null) { + logRequest(logger, method, url); + return; + } + + try { + String bodyAsJson = objectMapper.writeValueAsString(body); + logger.debug("Sending {} {} Body: {}", method.name(), url, bodyAsJson); + } catch (JsonProcessingException e) { + logRequest(logger, method, url); + logger.debug("Failed to parse object in logRequest. {}", body); + } + } + + public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url) { + logger.debug("Sending {} {}", method.name(), url); + } + + public static void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response, final Class entityClass) { + if (!logger.isDebugEnabled()) { + return; + } + if (response == null) { + logger.debug("Received {} {} response: null", method.name(), url); + return; + } + try { + response.bufferEntity(); + logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.readEntity(entityClass)); + } + catch (ProcessingException | IllegalStateException e) { + logger.debug("Received {} {} Status: {} . Failed to read response as {}", method.name(), url, response.getStatus(), entityClass.getName()); + } + } + + public static void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response) { + logResponse(logger, method, url, response, String.class); + } + + public static HttpServletRequest getHttpServletRequest(){ + return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); + } + + +} -- cgit 1.2.3-korg