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 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;
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;
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;
29 public class Logging {
34 public static final String HTTP_REQUESTS_OUTGOING = "http.requests.outgoing.";
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";
40 private static ObjectMapper objectMapper = new ObjectMapper();
42 public static String getMethodName() {
43 return getMethodName(0);
46 public static String getMethodCallerName() {
47 return getMethodName(1);
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)
56 .filter(not(frame -> frame.getClassName().equals(thisClassName)))
58 .map(StackTraceElement::getMethodName)
60 return caller.orElse("<unknonwn method name>");
63 public static EELFLogger getRequestsLogger(String serverName) {
64 return EELFLoggerDelegate.getLogger(HTTP_REQUESTS_OUTGOING +serverName);
67 public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url, final Object body) {
68 if (!logger.isDebugEnabled()) {
73 logRequest(logger, method, url);
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);
86 public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url) {
87 logger.debug("Sending {} {}", method.name(), url);
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()) {
94 if (response == null) {
95 logger.debug("Received {} {} response: null", method.name(), url);
99 response.bufferEntity();
100 logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.readEntity(entityClass));
102 catch (ProcessingException | IllegalStateException e) {
103 logger.debug("Received {} {} Status: {} . Failed to read response as {}", method.name(), url, response.getStatus(), entityClass.getName());
107 public static <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final HttpResponse<T> response) {
109 logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.getBody());
111 catch (ProcessingException | IllegalStateException e) {
112 logger.debug("Received {} {} Status: {} . Failed to read response", method.name(), url, response.getStatus());
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);
120 public static HttpServletRequest getHttpServletRequest(){
121 return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
124 public static String extractOrGenerateRequestId() {
126 return getHttpServletRequest().getHeader(REQUEST_ID_HEADER_KEY);
128 catch (IllegalStateException e) {
129 //in async jobs we don't have any HttpServletRequest
130 return UUID.randomUUID().toString();
134 public static void debugRequestDetails(Object requestDetails, final EELFLogger logger) {
135 if (logger.isDebugEnabled()) {
136 String requestDetailsAsString;
138 requestDetailsAsString = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(requestDetails);
139 } catch (JsonProcessingException e) {
140 requestDetailsAsString = "error: cannot stringify RequestDetails";
142 logger.debug("requestDetailsAsString: {}", requestDetailsAsString);
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);
153 final Throwable root = defaultIfNull(getRootCause(top), top);
155 String rootToString = root.toString();
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());
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)) {
170 final String topToString = top.toString();
171 if (topToString.contains(root.getClass().getName()) && topToString.contains(root.getLocalizedMessage())) {
174 return topToString + ": " + rootToString;