2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.utils;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.SerializationFeature;
27 import com.google.common.collect.ImmutableList;
28 import io.joshworks.restclient.http.HttpResponse;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.vid.exceptions.GenericUncheckedException;
31 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
32 import org.onap.portalsdk.core.util.SystemProperties;
33 import org.springframework.http.HttpMethod;
34 import org.springframework.web.context.request.RequestContextHolder;
35 import org.springframework.web.context.request.ServletRequestAttributes;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.ws.rs.ProcessingException;
39 import javax.ws.rs.core.Response;
40 import java.util.Arrays;
41 import java.util.Optional;
42 import java.util.UUID;
44 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
45 import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCause;
46 import static org.apache.commons.lang3.exception.ExceptionUtils.getThrowableList;
47 import static org.onap.vid.utils.Streams.not;
49 public class Logging {
54 public static final String HTTP_REQUESTS_OUTGOING = "http.requests.outgoing.";
56 public static final String REQUEST_ID_HEADER_KEY = SystemProperties.ECOMP_REQUEST_ID;
57 public static final String ONAP_REQUEST_ID_HEADER_KEY = "X-ONAP-RequestID";
60 private static ObjectMapper objectMapper = new ObjectMapper();
62 public static String getMethodName() {
63 return getMethodName(0);
66 public static String getMethodCallerName() {
67 return getMethodName(1);
70 private static String getMethodName(int depth) {
71 final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
72 String thisClassName = stackTrace[1].getClassName();
73 final Optional<String> caller =
74 Arrays.stream(stackTrace)
76 .filter(not(frame -> frame.getClassName().equals(thisClassName)))
78 .map(StackTraceElement::getMethodName)
80 return caller.orElse("<unknonwn method name>");
83 public static EELFLogger getRequestsLogger(String serverName) {
84 return EELFLoggerDelegate.getLogger(HTTP_REQUESTS_OUTGOING +serverName);
87 public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url, final Object body) {
88 if (!logger.isDebugEnabled()) {
93 logRequest(logger, method, url);
98 String bodyAsJson = objectMapper.writeValueAsString(body);
99 logger.debug("Sending {} {} Body: {}", method.name(), url, bodyAsJson);
100 } catch (JsonProcessingException e) {
101 logRequest(logger, method, url);
102 logger.debug("Failed to parse object in logRequest. {}", body);
106 public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url) {
107 logger.debug("Sending {} {}", method.name(), url);
110 public static <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response, final Class<T> entityClass) {
111 if (!logger.isDebugEnabled()) {
114 if (response == null) {
115 logger.debug("Received {} {} response: null", method.name(), url);
119 response.bufferEntity();
120 logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.readEntity(entityClass));
122 catch (Exception e) {
123 logger.debug("Received {} {} Status: {} . Failed to read response as {}", method.name(), url, response.getStatus(), entityClass.getName());
127 public static <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final HttpResponse<T> response) {
129 logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.getBody());
131 catch (Exception e) {
132 logger.debug("Received {} {} Status: {} . Failed to read response", method.name(), url, response.getStatus());
136 public static void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response) {
137 logResponse(logger, method, url, response, String.class);
140 public static HttpServletRequest getHttpServletRequest(){
141 return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
144 public static String extractOrGenerateRequestId() {
146 return getHttpServletRequest().getHeader(REQUEST_ID_HEADER_KEY);
148 catch (IllegalStateException e) {
149 //in async jobs we don't have any HttpServletRequest
150 return UUID.randomUUID().toString();
154 public static void debugRequestDetails(Object requestDetails, final EELFLogger logger) {
155 if (logger.isDebugEnabled()) {
156 String requestDetailsAsString;
158 requestDetailsAsString = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(requestDetails);
159 } catch (JsonProcessingException e) {
160 requestDetailsAsString = "error: cannot stringify RequestDetails";
162 logger.debug("requestDetailsAsString: {}", requestDetailsAsString);
166 public static String exceptionToDescription(Throwable exceptionToDescribe) {
167 // Ignore top-most GenericUnchecked or Runtime exceptions that has no added message
168 final Throwable top = getThrowableList(exceptionToDescribe).stream()
169 .filter(not(e -> ImmutableList.of(GenericUncheckedException.class, RuntimeException.class).contains(e.getClass())
170 && StringUtils.equals(e.getMessage(), e.getCause() == null ? null : e.getCause().toString())))
171 .findFirst().orElse(exceptionToDescribe);
173 final Throwable root = defaultIfNull(getRootCause(top), top);
175 String rootToString = root.toString();
177 // nullPointer description will include some context
178 if (root.getClass().equals(NullPointerException.class) && root.getStackTrace().length > 0) {
179 rootToString = String.format("NullPointerException at %s:%d",
180 root.getStackTrace()[0].getFileName(),
181 root.getStackTrace()[0].getLineNumber());
184 // if input is a single exception, without cause: top.toString
185 // else: return top.toString + root.toString
186 // but not if root is already described in top.toString
187 if (top.equals(root)) {
190 final String topToString = top.toString();
191 if (topToString.contains(root.getClass().getName()) && topToString.contains(root.getLocalizedMessage())) {
194 return topToString + ": " + rootToString;