Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / utils / Logging.java
1 package org.onap.vid.utils;
2
3 import com.att.eelf.configuration.EELFLogger;
4 import com.fasterxml.jackson.core.JsonProcessingException;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import com.fasterxml.jackson.databind.SerializationFeature;
7 import com.google.common.collect.ImmutableList;
8 import io.joshworks.restclient.http.HttpResponse;
9 import org.apache.commons.lang3.StringUtils;
10 import org.onap.vid.exceptions.GenericUncheckedException;
11 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
12 import org.onap.portalsdk.core.util.SystemProperties;
13 import org.springframework.http.HttpMethod;
14 import org.springframework.web.context.request.RequestContextHolder;
15 import org.springframework.web.context.request.ServletRequestAttributes;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.ws.rs.ProcessingException;
19 import javax.ws.rs.core.Response;
20 import java.util.Arrays;
21 import java.util.Optional;
22 import java.util.UUID;
23
24 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
25 import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCause;
26 import static org.apache.commons.lang3.exception.ExceptionUtils.getThrowableList;
27 import static org.onap.vid.utils.Streams.not;
28
29 public class Logging {
30
31     private Logging() {
32     }
33
34     public static final String HTTP_REQUESTS_OUTGOING = "http.requests.outgoing.";
35
36     public static final String REQUEST_ID_HEADER_KEY = SystemProperties.ECOMP_REQUEST_ID;
37     public static final String ONAP_REQUEST_ID_HEADER_KEY = "X-ONAP-RequestID";
38
39
40     private static ObjectMapper objectMapper = new ObjectMapper();
41
42     public static String getMethodName() {
43         return getMethodName(0);
44     }
45
46     public static String getMethodCallerName() {
47         return getMethodName(1);
48     }
49
50     private static String getMethodName(int depth) {
51         final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
52         String thisClassName = stackTrace[1].getClassName();
53         final Optional<String> caller =
54                 Arrays.stream(stackTrace)
55                         .skip(1)
56                         .filter(not(frame -> frame.getClassName().equals(thisClassName)))
57                         .skip(depth)
58                         .map(StackTraceElement::getMethodName)
59                         .findFirst();
60         return caller.orElse("<unknonwn method name>");
61     }
62
63     public static EELFLogger getRequestsLogger(String serverName) {
64         return EELFLoggerDelegate.getLogger(HTTP_REQUESTS_OUTGOING +serverName);
65     }
66
67     public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url, final Object body) {
68         if (!logger.isDebugEnabled()) {
69             return;
70         }
71
72         if (body == null) {
73             logRequest(logger, method, url);
74             return;
75         }
76
77         try {
78             String bodyAsJson = objectMapper.writeValueAsString(body);
79             logger.debug("Sending  {} {} Body: {}", method.name(), url, bodyAsJson);
80         } catch (JsonProcessingException e) {
81             logRequest(logger, method, url);
82             logger.debug("Failed to parse object in logRequest. {}", body);
83         }
84     }
85
86     public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url) {
87         logger.debug("Sending  {} {}", method.name(), url);
88     }
89
90     public static <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response, final Class<T> entityClass) {
91         if (!logger.isDebugEnabled()) {
92             return;
93         }
94         if (response == null) {
95             logger.debug("Received {} {} response: null", method.name(), url);
96             return;
97         }
98         try {
99             response.bufferEntity();
100             logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.readEntity(entityClass));
101         }
102         catch (ProcessingException | IllegalStateException e) {
103             logger.debug("Received {} {} Status: {} . Failed to read response as {}", method.name(), url, response.getStatus(), entityClass.getName());
104         }
105     }
106
107     public static <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final HttpResponse<T> response) {
108         try {
109             logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.getBody());
110         }
111         catch (ProcessingException | IllegalStateException e) {
112             logger.debug("Received {} {} Status: {} . Failed to read response", method.name(), url, response.getStatus());
113         }
114     }
115
116     public static void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response) {
117         logResponse(logger, method, url, response, String.class);
118     }
119
120     public static HttpServletRequest getHttpServletRequest(){
121         return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
122     }
123
124     public static String extractOrGenerateRequestId() {
125         try {
126             return getHttpServletRequest().getHeader(REQUEST_ID_HEADER_KEY);
127         }
128         catch (IllegalStateException e) {
129             //in async jobs we don't have any HttpServletRequest
130             return UUID.randomUUID().toString();
131         }
132     }
133
134     public static void debugRequestDetails(Object requestDetails, final EELFLogger logger) {
135         if (logger.isDebugEnabled()) {
136             String requestDetailsAsString;
137             try {
138                 requestDetailsAsString = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(requestDetails);
139             } catch (JsonProcessingException e) {
140                 requestDetailsAsString = "error: cannot stringify RequestDetails";
141             }
142             logger.debug("requestDetailsAsString: {}", requestDetailsAsString);
143         }
144     }
145
146     public static String exceptionToDescription(Throwable exceptionToDescribe) {
147         // Ignore top-most GenericUnchecked or Runtime exceptions that has no added message
148         final Throwable top = getThrowableList(exceptionToDescribe).stream()
149                 .filter(not(e -> ImmutableList.of(GenericUncheckedException.class, RuntimeException.class).contains(e.getClass())
150                         && StringUtils.equals(e.getMessage(), e.getCause() == null ? null : e.getCause().toString())))
151                 .findFirst().orElse(exceptionToDescribe);
152
153         final Throwable root = defaultIfNull(getRootCause(top), top);
154
155         String rootToString = root.toString();
156
157         // nullPointer description will include some context
158         if (root.getClass().equals(NullPointerException.class) && root.getStackTrace().length > 0) {
159             rootToString = String.format("NullPointerException at %s:%d",
160                     root.getStackTrace()[0].getFileName(),
161                     root.getStackTrace()[0].getLineNumber());
162         }
163
164         // if input is a single exception, without cause: top.toString
165         // else: return top.toString + root.toString
166         //       but not if root is already described in top.toString
167         if (top.equals(root)) {
168             return rootToString;
169         } else {
170             final String topToString = top.toString();
171             if (topToString.contains(root.getClass().getName()) && topToString.contains(root.getLocalizedMessage())) {
172                 return topToString;
173             } else {
174                 return topToString + ": " + rootToString;
175             }
176         }
177     }
178
179
180 }