Moving Historical Queries to spring boot
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / logging / LoggingUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.datarouter.logging;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.ws.rs.core.HttpHeaders;
25 import javax.ws.rs.core.Response;
26
27 import org.onap.aai.cl.api.LogFields;
28 import org.onap.aai.cl.api.LogLine;
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.mdc.MdcContext;
31 import org.onap.aai.datarouter.util.DataRouterConstants;
32 import org.slf4j.MDC;
33
34 public class LoggingUtil {
35   public static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
36     String fromIp = httpReq.getRemoteAddr();
37     String fromAppId = "";
38     String transId = null;
39
40     if (headers.getRequestHeaders().getFirst("X-FromAppId") != null) {
41       fromAppId = headers.getRequestHeaders().getFirst("X-FromAppId");
42     }
43
44     if ((headers.getRequestHeaders().getFirst("X-TransactionId") == null)
45         || headers.getRequestHeaders().getFirst("X-TransactionId").isEmpty()) {
46       transId = java.util.UUID.randomUUID().toString();
47     } else {
48       transId = headers.getRequestHeaders().getFirst("X-TransactionId");
49     }
50
51     MdcContext.initialize(transId, DataRouterConstants.DATA_ROUTER_SERVICE_NAME, "", fromAppId,
52         fromIp);
53   }
54
55   public static void logRestRequest(Logger logger, Logger auditLogger, HttpServletRequest req, Response response) {
56     String respStatusString = "";
57     if (Response.Status.fromStatusCode(response.getStatus()) != null) {
58       respStatusString = Response.Status.fromStatusCode(response.getStatus()).toString();
59     }
60
61     // Generate error log
62     logger.info(DataRouterMsgs.PROCESS_REST_REQUEST, req.getMethod(), req.getRequestURL().toString(),
63         req.getRemoteHost(), Integer.toString(response.getStatus()));
64
65     // Generate audit log.
66     auditLogger.info(DataRouterMsgs.PROCESS_REST_REQUEST,
67         new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatus())
68             .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, respStatusString),
69         (req != null) ? req.getMethod() : "Unknown", (req != null) ? req.getRequestURL().toString() : "Unknown",
70         (req != null) ? req.getRemoteHost() : "Unknown", Integer.toString(response.getStatus()) + " payload: "
71             + (response.getEntity() == null ? "" : response.getEntity().toString()));
72     MDC.clear();
73   }
74
75   public static String setDuration(long startTime, long stopTime) {
76     return String.valueOf(stopTime - startTime);
77   }
78 }