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 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;
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;
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;
28 public class Logging {
33 public static final String HTTP_REQUESTS_OUTGOING = "http.requests.outgoing.";
35 public static final String REQUEST_ID_HEADER_KEY = SystemProperties.ECOMP_REQUEST_ID;
37 private static ObjectMapper objectMapper = new ObjectMapper();
39 public static String getMethodName() {
40 return getMethodName(0);
43 public static String getMethodCallerName() {
44 return getMethodName(1);
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)
53 .filter(not(frame -> frame.getClassName().equals(thisClassName)))
55 .map(StackTraceElement::getMethodName)
57 return caller.orElse("<unknonwn method name>");
60 public static EELFLogger getRequestsLogger(String serverName) {
61 return EELFLoggerDelegate.getLogger(HTTP_REQUESTS_OUTGOING +serverName);
64 public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url, final Object body) {
65 if (!logger.isDebugEnabled()) {
70 logRequest(logger, method, url);
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);
83 public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url) {
84 logger.debug("Sending {} {}", method.name(), url);
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()) {
91 if (response == null) {
92 logger.debug("Received {} {} response: null", method.name(), url);
96 response.bufferEntity();
97 logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.readEntity(entityClass));
99 catch (ProcessingException | IllegalStateException e) {
100 logger.debug("Received {} {} Status: {} . Failed to read response as {}", method.name(), url, response.getStatus(), entityClass.getName());
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);
108 public static HttpServletRequest getHttpServletRequest(){
109 return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
112 public static String extractOrGenerateRequestId() {
114 return getHttpServletRequest().getHeader(REQUEST_ID_HEADER_KEY);
116 catch (IllegalStateException e) {
117 //in async jobs we don't have any HttpServletRequest
118 return UUID.randomUUID().toString();
122 public static void debugRequestDetails(Object requestDetails, final EELFLogger logger) {
123 if (logger.isDebugEnabled()) {
124 String requestDetailsAsString;
126 requestDetailsAsString = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(requestDetails);
127 } catch (JsonProcessingException e) {
128 requestDetailsAsString = "error: cannot stringify RequestDetails";
130 logger.debug("requestDetailsAsString: {}", requestDetailsAsString);
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);
141 final Throwable root = defaultIfNull(getRootCause(top), top);
143 String rootToString = root.toString();
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());
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)) {
158 final String topToString = top.toString();
159 if (topToString.contains(root.getClass().getName()) && topToString.contains(root.getLocalizedMessage())) {
162 return topToString + ": " + rootToString;