Port champ-microservice project restructure
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / service / 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.onap.champ.service.logging;
25
26 import org.onap.aai.cl.api.LogFields;
27 import org.onap.aai.cl.api.LogLine;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.mdc.MdcContext;
30
31 import org.slf4j.MDC;
32
33 import java.io.PrintWriter;
34 import java.io.StringWriter;
35
36 import javax.servlet.http.HttpServletRequest;
37 import javax.ws.rs.core.HttpHeaders;
38 import javax.ws.rs.core.Response;
39
40 public class LoggingUtil {
41   /**
42    * Initializes mdc context.
43    */
44   public static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
45     String fromIp = httpReq.getRemoteAddr();
46     String fromAppId = "";
47     String transId = null;
48
49     if (headers.getRequestHeaders().getFirst("X-FromAppId") != null) {
50       fromAppId = headers.getRequestHeaders().getFirst("X-FromAppId");
51     }
52
53     if ((headers.getRequestHeaders().getFirst("X-TransactionId") == null)
54         || headers.getRequestHeaders().getFirst("X-TransactionId").isEmpty()) {
55       transId = java.util.UUID.randomUUID().toString();
56     } else {
57       transId = headers.getRequestHeaders().getFirst("X-TransactionId");
58     }
59
60     MdcContext.initialize(transId, "ChampService", "", fromAppId, fromIp);
61   }
62
63   /**
64    * Logs the rest request.
65    */
66   public static void logRestRequest(Logger logger, Logger auditLogger, HttpServletRequest req, Response response) {
67     String respStatusString = "";
68     if (Response.Status.fromStatusCode(response.getStatus()) != null) {
69       respStatusString = Response.Status.fromStatusCode(response.getStatus()).toString();
70     }
71
72     // Generate error log
73     logger.info(ChampMsgs.PROCESS_REST_REQUEST, req.getMethod(), req.getRequestURL().toString(),
74         req.getRemoteHost(), Integer.toString(response.getStatus()));
75
76     // Generate audit log.
77     auditLogger.info(ChampMsgs.PROCESS_REST_REQUEST,
78         new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatus())
79             .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, respStatusString),
80         (req != null) ? req.getMethod() : "Unknown", (req != null) ? req.getRequestURL().toString() : "Unknown",
81         (req != null) ? req.getRemoteHost() : "Unknown", Integer.toString(response.getStatus()) + " payload: "
82             + (response.getEntity() == null ? "" : response.getEntity().toString()));
83     MDC.clear();
84   }
85   
86   public static void logInternalError(Logger logger, Exception ex) {
87     StringWriter writer = new StringWriter();
88     PrintWriter printWriter = new PrintWriter(writer);
89     ex.printStackTrace(printWriter);
90     logger.error(ChampMsgs.CHAMP_DATA_SERVICE_ERROR, "Internal error: " + ex.getMessage() + "\n" + writer.toString());
91   }
92 }