2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.champ.service.logging;
24 import org.onap.aai.cl.api.LogFields;
25 import org.onap.aai.cl.api.LogLine;
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.mdc.MdcContext;
31 import java.io.PrintWriter;
32 import java.io.StringWriter;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.core.HttpHeaders;
36 import javax.ws.rs.core.Response;
38 public class LoggingUtil {
40 * Initializes mdc context.
42 public static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
43 String fromIp = httpReq.getRemoteAddr();
44 String fromAppId = "";
45 String transId = null;
47 if (headers.getRequestHeaders().getFirst("X-FromAppId") != null) {
48 fromAppId = headers.getRequestHeaders().getFirst("X-FromAppId");
51 if ((headers.getRequestHeaders().getFirst("X-TransactionId") == null)
52 || headers.getRequestHeaders().getFirst("X-TransactionId").isEmpty()) {
53 transId = java.util.UUID.randomUUID().toString();
55 transId = headers.getRequestHeaders().getFirst("X-TransactionId");
58 MdcContext.initialize(transId, "ChampService", "", fromAppId, fromIp);
62 * Logs the rest request.
64 public static void logRestRequest(Logger logger, Logger auditLogger, HttpServletRequest req, Response response) {
65 String respStatusString = "";
66 if (Response.Status.fromStatusCode(response.getStatus()) != null) {
67 respStatusString = Response.Status.fromStatusCode(response.getStatus()).toString();
71 logger.info(ChampMsgs.PROCESS_REST_REQUEST, req.getMethod(), req.getRequestURL().toString(),
72 req.getRemoteHost(), Integer.toString(response.getStatus()));
74 // Generate audit log.
75 auditLogger.info(ChampMsgs.PROCESS_REST_REQUEST,
76 new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatus())
77 .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, respStatusString),
78 (req != null) ? req.getMethod() : "Unknown", (req != null) ? req.getRequestURL().toString() : "Unknown",
79 (req != null) ? req.getRemoteHost() : "Unknown", Integer.toString(response.getStatus()) + " payload: "
80 + (response.getEntity() == null ? "" : response.getEntity().toString()));
84 public static void logInternalError(Logger logger, Exception ex) {
85 StringWriter writer = new StringWriter();
86 PrintWriter printWriter = new PrintWriter(writer);
87 ex.printStackTrace(printWriter);
88 logger.error(ChampMsgs.CHAMP_DATA_SERVICE_ERROR, "Internal error: " + ex.getMessage() + "\n" + writer.toString());