f9894d1aa7cfe0e7392c9f6cc6325f47a1be3d24
[vid.git] / vid-app-common / src / main / java / org / onap / vid / utils / Logging.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.vid.utils;
22
23 import static java.util.Collections.emptyMap;
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 import com.att.eelf.configuration.EELFLogger;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.SerializationFeature;
33 import com.google.common.collect.ImmutableList;
34 import io.joshworks.restclient.http.HttpResponse;
35 import java.nio.charset.StandardCharsets;
36 import java.util.Arrays;
37 import java.util.Map;
38 import java.util.Optional;
39 import java.util.UUID;
40 import java.util.concurrent.Callable;
41 import java.util.function.Function;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.ws.rs.core.Response;
44 import org.apache.commons.io.IOUtils;
45 import org.apache.commons.lang3.StringUtils;
46 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
47 import org.onap.portalsdk.core.util.SystemProperties;
48 import org.onap.vid.exceptions.GenericUncheckedException;
49 import org.onap.vid.utils.Unchecked.UncheckedThrowingSupplier;
50 import org.slf4j.MDC;
51 import org.springframework.http.HttpMethod;
52 import org.springframework.stereotype.Service;
53 import org.springframework.web.context.request.RequestContextHolder;
54 import org.springframework.web.context.request.ServletRequestAttributes;
55
56 @Service
57 public class Logging {
58
59     public static final String HTTP_REQUESTS_OUTGOING = "http.requests.outgoing.";
60
61     public static final String REQUEST_ID_HEADER_KEY = SystemProperties.ECOMP_REQUEST_ID;
62     public static final String ONAP_REQUEST_ID_HEADER_KEY = "X-ONAP-RequestID";
63
64
65     private static ObjectMapper objectMapper = new ObjectMapper();
66
67     public static String getMethodName() {
68         return getMethodName(0);
69     }
70
71     public static String getMethodCallerName() {
72         return getMethodName(1);
73     }
74
75     private static String getMethodName(int depth) {
76         final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
77         String thisClassName = stackTrace[1].getClassName();
78         final Optional<String> caller =
79                 Arrays.stream(stackTrace)
80                         .skip(1)
81                         .filter(not(frame -> frame.getClassName().equals(thisClassName)))
82                         .skip(depth)
83                         .map(StackTraceElement::getMethodName)
84                         .findFirst();
85         return caller.orElse("<unknonwn method name>");
86     }
87
88     public static EELFLogger getRequestsLogger(String serverName) {
89         return EELFLoggerDelegate.getLogger(HTTP_REQUESTS_OUTGOING +serverName);
90     }
91
92     public void logRequest(final EELFLogger logger, final HttpMethod method, final String url, final Object body) {
93         if (!logger.isDebugEnabled()) {
94             return;
95         }
96
97         if (body == null) {
98             logRequest(logger, method, url);
99             return;
100         }
101
102         try {
103             String bodyAsJson = objectMapper.writeValueAsString(body);
104             logger.debug("Sending  {} {} Body: {}", method.name(), url, bodyAsJson);
105         } catch (JsonProcessingException e) {
106             logRequest(logger, method, url);
107             logger.debug("Failed to parse object in logRequest. {}", body);
108         }
109     }
110
111     public void logRequest(final EELFLogger logger, final HttpMethod method, final String url) {
112         logger.debug("Sending  {} {}", method.name(), url);
113     }
114
115     public <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response, final Class<T> entityClass) {
116         if (!logger.isDebugEnabled()) {
117             return;
118         }
119         if (response == null) {
120             logger.debug("Received {} {} response: null", method.name(), url);
121             return;
122         }
123         try {
124             response.bufferEntity();
125             logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.readEntity(entityClass));
126         }
127         catch (Exception e) {
128             logger.debug("Received {} {} Status: {} . Failed to read response as {}", method.name(), url, response.getStatus(), entityClass.getName());
129         }
130     }
131
132     public <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final HttpResponse<T> response) {
133         try {
134             logger.debug("Received {} {} Status: {} . Body: {}", method.name(),
135                 url, response.getStatus(), IOUtils.toString(response.getRawBody(), StandardCharsets.UTF_8));
136             response.getRawBody().reset();
137         }
138         catch (Exception e) {
139             logger.debug("Received {} {} Status: {} . Failed to read response", method.name(), url, response.getStatus());
140         }
141     }
142
143     public void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response) {
144         logResponse(logger, method, url, response, String.class);
145     }
146
147     public static HttpServletRequest getHttpServletRequest(){
148         return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
149     }
150
151     public static String extractOrGenerateRequestId() {
152         try {
153             return getHttpServletRequest().getHeader(REQUEST_ID_HEADER_KEY);
154         }
155         catch (IllegalStateException e) {
156             //in async jobs we don't have any HttpServletRequest
157             return UUID.randomUUID().toString();
158         }
159     }
160
161     public static void debugRequestDetails(Object requestDetails, final EELFLogger logger) {
162         if (logger.isDebugEnabled()) {
163             String requestDetailsAsString;
164             try {
165                 requestDetailsAsString = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(requestDetails);
166             } catch (JsonProcessingException e) {
167                 requestDetailsAsString = "error: cannot stringify RequestDetails";
168             }
169             logger.debug("requestDetailsAsString: {}", requestDetailsAsString);
170         }
171     }
172
173     public static String exceptionToDescription(Throwable exceptionToDescribe) {
174         // Ignore top-most GenericUnchecked or Runtime exceptions that has no added message
175         final Throwable top = getThrowableList(exceptionToDescribe).stream()
176                 .filter(not(e -> ImmutableList.of(GenericUncheckedException.class, RuntimeException.class).contains(e.getClass())
177                         && StringUtils.equals(e.getMessage(), e.getCause() == null ? null : e.getCause().toString())))
178                 .findFirst().orElse(exceptionToDescribe);
179
180         final Throwable root = defaultIfNull(getRootCause(top), top);
181
182         String rootToString = root.toString();
183
184         // nullPointer description will include some context
185         if (root.getClass().equals(NullPointerException.class) && root.getStackTrace().length > 0) {
186             rootToString = String.format("NullPointerException at %s:%d",
187                     root.getStackTrace()[0].getFileName(),
188                     root.getStackTrace()[0].getLineNumber());
189         }
190
191         // if input is a single exception, without cause: top.toString
192         // else: return top.toString + root.toString
193         //       but not if root is already described in top.toString
194         if (top.equals(root)) {
195             return rootToString;
196         } else {
197             final String topToString = top.toString();
198             if (topToString.contains(root.getClass().getName()) && topToString.contains(root.getLocalizedMessage())) {
199                 return topToString;
200             } else {
201                 return topToString + ": " + rootToString;
202             }
203         }
204     }
205
206     /**
207      * in order to be able to write the correct data while creating the node on a new thread save a copy of the current
208      * thread's context map, with keys and values of type String.
209      */
210     public <T> Callable<T> withMDC(Map<String, String> copyOfParentMDC, Callable<T> callable) {
211         return () -> withMDCInternal(copyOfParentMDC, callable::call);
212     }
213
214     /**
215      * in order to be able to write the correct data while creating the node on a new thread save a copy of the current
216      * thread's context map, with keys and values of type String.
217      */
218     public <T, U> Function<T, U> withMDC(Map<String, String> copyOfParentMDC, Function<T, U> function) {
219         return t -> withMDCInternal(copyOfParentMDC, () -> function.apply(t));
220     }
221
222     <T> T withMDCInternal(Map<String, String> copyOfParentMDC, UncheckedThrowingSupplier<T> supplier) {
223         try {
224             MDC.setContextMap(defaultIfNull(copyOfParentMDC, emptyMap()));
225             return supplier.get();
226         } finally {
227             MDC.clear();
228         }
229     }
230
231 }