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