1 package org.onap.vid.utils;
3 import com.att.eelf.configuration.EELFLogger;
4 import com.fasterxml.jackson.core.JsonProcessingException;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
7 import org.onap.portalsdk.core.util.SystemProperties;
8 import org.springframework.http.HttpMethod;
9 import org.springframework.web.context.request.RequestContextHolder;
10 import org.springframework.web.context.request.ServletRequestAttributes;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.ws.rs.ProcessingException;
14 import javax.ws.rs.core.Response;
15 import java.util.Arrays;
16 import java.util.Optional;
18 import static org.onap.vid.utils.Streams.not;
20 public class Logging {
25 public static final String HTTP_REQUESTS_OUTGOING = "http.requests.outgoing.";
27 public static final String requestIdHeaderKey = SystemProperties.ECOMP_REQUEST_ID;
29 private static ObjectMapper objectMapper = new ObjectMapper();
31 public static String getMethodName() {
32 return getMethodName(0);
35 public static String getMethodCallerName() {
36 return getMethodName(1);
39 private static String getMethodName(int depth) {
40 final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
41 String thisClassName = stackTrace[1].getClassName();
42 final Optional<String> caller =
43 Arrays.stream(stackTrace)
45 .filter(not(frame -> frame.getClassName().equals(thisClassName)))
47 .map(StackTraceElement::getMethodName)
49 return caller.orElse("<unknonwn method name>");
52 public static EELFLogger getRequestsLogger(String serverName) {
53 return EELFLoggerDelegate.getLogger(HTTP_REQUESTS_OUTGOING +serverName);
56 public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url, final Object body) {
57 if (!logger.isDebugEnabled()) {
62 logRequest(logger, method, url);
67 String bodyAsJson = objectMapper.writeValueAsString(body);
68 logger.debug("Sending {} {} Body: {}", method.name(), url, bodyAsJson);
69 } catch (JsonProcessingException e) {
70 logRequest(logger, method, url);
71 logger.debug("Failed to parse object in logRequest. {}", body);
75 public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url) {
76 logger.debug("Sending {} {}", method.name(), url);
79 public static <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response, final Class<T> entityClass) {
80 if (!logger.isDebugEnabled()) {
83 if (response == null) {
84 logger.debug("Received {} {} response: null", method.name(), url);
88 response.bufferEntity();
89 logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.readEntity(entityClass));
91 catch (ProcessingException | IllegalStateException e) {
92 logger.debug("Received {} {} Status: {} . Failed to read response as {}", method.name(), url, response.getStatus(), entityClass.getName());
96 public static void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response) {
97 logResponse(logger, method, url, response, String.class);
100 public static HttpServletRequest getHttpServletRequest(){
101 return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();