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