|
10 | 10 |
|
11 | 11 | import jakarta.servlet.http.HttpServletRequest;
|
12 | 12 | import jakarta.servlet.http.HttpServletResponse;
|
| 13 | + |
13 | 14 | import java.time.Instant;
|
14 | 15 | import java.util.HashMap;
|
15 | 16 | import java.util.List;
|
16 | 17 | import java.util.Map;
|
17 | 18 |
|
18 | 19 | @Slf4j
|
19 | 20 | public final class LogHelper {
|
20 |
| - private static final ObjectMapper mapper = new ObjectMapper(); |
21 |
| - private static final String EXCEPTION = "Exception "; |
| 21 | + private static final ObjectMapper mapper = new ObjectMapper(); |
| 22 | + private static final String EXCEPTION = "Exception "; |
22 | 23 |
|
23 |
| - private LogHelper() { |
| 24 | + private LogHelper() { |
24 | 25 |
|
25 |
| - } |
| 26 | + } |
26 | 27 |
|
27 |
| - public static void logServerHttpReqResponseDetails(@NonNull final HttpServletRequest request, final HttpServletResponse response, final boolean logging) { |
28 |
| - if (!logging) return; |
29 |
| - try { |
30 |
| - final int status = response.getStatus(); |
31 |
| - val totalTime = Instant.now().toEpochMilli() - (Long) request.getAttribute("startTime"); |
32 |
| - final Map<String, Object> httpMap = new HashMap<>(); |
33 |
| - httpMap.put("server_http_response_code", status); |
34 |
| - httpMap.put("server_http_request_method", request.getMethod()); |
35 |
| - httpMap.put("server_http_query_params", request.getQueryString()); |
36 |
| - val correlationID = request.getHeader(EducCourseApiConstants.CORRELATION_ID); |
37 |
| - if (correlationID != null) { |
38 |
| - httpMap.put("correlation_id", correlationID); |
39 |
| - } |
40 |
| - httpMap.put("server_http_request_url", String.valueOf(request.getRequestURL())); |
41 |
| - httpMap.put("server_http_request_processing_time_ms", totalTime); |
42 |
| - httpMap.put("server_http_request_payload", String.valueOf(request.getAttribute("payload"))); |
43 |
| - httpMap.put("server_http_request_remote_address", request.getRemoteAddr()); |
44 |
| - httpMap.put("server_http_request_client_name", StringUtils.trimToEmpty(request.getHeader("X-Client-Name"))); |
45 |
| - MDC.putCloseable("httpEvent", mapper.writeValueAsString(httpMap)); |
46 |
| - log.info(""); |
47 |
| - MDC.clear(); |
48 |
| - } catch (final Exception exception) { |
49 |
| - log.error(EXCEPTION, exception); |
| 28 | + public static void logServerHttpReqResponseDetails(@NonNull final HttpServletRequest request, final HttpServletResponse response, final boolean logging) { |
| 29 | + if (!logging) return; |
| 30 | + try { |
| 31 | + final int status = response.getStatus(); |
| 32 | + val totalTime = Instant.now().toEpochMilli() - (Long) request.getAttribute("startTime"); |
| 33 | + final Map<String, Object> httpMap = new HashMap<>(); |
| 34 | + httpMap.put("server_http_response_code", status); |
| 35 | + httpMap.put("server_http_request_method", request.getMethod()); |
| 36 | + httpMap.put("server_http_query_params", request.getQueryString()); |
| 37 | + val correlationID = request.getHeader(EducCourseApiConstants.CORRELATION_ID); |
| 38 | + if (correlationID != null) { |
| 39 | + httpMap.put("correlation_id", correlationID); |
| 40 | + } |
| 41 | + val requestSource = request.getHeader(EducCourseApiConstants.REQUEST_SOURCE); |
| 42 | + if (requestSource != null) { |
| 43 | + httpMap.put("request_source", requestSource); |
| 44 | + } |
| 45 | + httpMap.put("server_http_request_url", String.valueOf(request.getRequestURL())); |
| 46 | + httpMap.put("server_http_request_processing_time_ms", totalTime); |
| 47 | + httpMap.put("server_http_request_payload", String.valueOf(request.getAttribute("payload"))); |
| 48 | + httpMap.put("server_http_request_remote_address", request.getRemoteAddr()); |
| 49 | + httpMap.put("server_http_request_client_name", StringUtils.trimToEmpty(request.getHeader("X-Client-Name"))); |
| 50 | + MDC.putCloseable("httpEvent", mapper.writeValueAsString(httpMap)); |
| 51 | + log.info(""); |
| 52 | + MDC.clear(); |
| 53 | + } catch (final Exception exception) { |
| 54 | + log.error(EXCEPTION, exception); |
| 55 | + } |
50 | 56 | }
|
51 |
| - } |
52 | 57 |
|
53 |
| - /** |
54 |
| - * WebClient to call other REST APIs |
55 |
| - * |
56 |
| - * @param method |
57 |
| - * @param url |
58 |
| - * @param responseCode |
59 |
| - * @param correlationID |
60 |
| - */ |
61 |
| - public static void logClientHttpReqResponseDetails(@NonNull final HttpMethod method, final String url, final int responseCode, final List<String> correlationID, final boolean logging) { |
62 |
| - if (!logging) return; |
63 |
| - try { |
64 |
| - final Map<String, Object> httpMap = new HashMap<>(); |
65 |
| - httpMap.put("client_http_response_code", responseCode); |
66 |
| - httpMap.put("client_http_request_method", method.toString()); |
67 |
| - httpMap.put("client_http_request_url", url); |
68 |
| - if (correlationID != null) { |
69 |
| - httpMap.put("correlation_id", String.join(",", correlationID)); |
70 |
| - } |
71 |
| - MDC.putCloseable("httpEvent", mapper.writeValueAsString(httpMap)); |
72 |
| - log.info(""); |
73 |
| - MDC.clear(); |
74 |
| - } catch (final Exception exception) { |
75 |
| - log.error(EXCEPTION, exception); |
| 58 | + /** |
| 59 | + * WebClient to call other REST APIs |
| 60 | + * |
| 61 | + * @param method |
| 62 | + * @param url |
| 63 | + * @param responseCode |
| 64 | + * @param correlationID |
| 65 | + */ |
| 66 | + public static void logClientHttpReqResponseDetails(@NonNull final HttpMethod method, final String url, final int responseCode, final List<String> correlationID, final List<String> requestSource, final boolean logging) { |
| 67 | + if (!logging) return; |
| 68 | + try { |
| 69 | + final Map<String, Object> httpMap = new HashMap<>(); |
| 70 | + httpMap.put("client_http_response_code", responseCode); |
| 71 | + httpMap.put("client_http_request_method", method.toString()); |
| 72 | + httpMap.put("client_http_request_url", url); |
| 73 | + if (correlationID != null) { |
| 74 | + httpMap.put("correlation_id", String.join(",", correlationID)); |
| 75 | + } |
| 76 | + if (requestSource != null) { |
| 77 | + httpMap.put("request_source", String.join(",", requestSource)); |
| 78 | + } |
| 79 | + MDC.putCloseable("httpEvent", mapper.writeValueAsString(httpMap)); |
| 80 | + log.info(""); |
| 81 | + MDC.clear(); |
| 82 | + } catch (final Exception exception) { |
| 83 | + log.error(EXCEPTION, exception); |
| 84 | + } |
76 | 85 | }
|
77 |
| - } |
78 | 86 |
|
79 | 87 | /**
|
80 | 88 | * the event is a json string.
|
|
0 commit comments