2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.logging;
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 import org.onap.crud.util.CrudServiceConstants;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.core.HttpHeaders;
35 import javax.ws.rs.core.Response;
37 public class LoggingUtil {
39 * Initializes mdc context.
41 public static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
42 String fromIp = httpReq.getRemoteAddr();
43 String fromAppId = "";
44 String transId = null;
46 if (headers.getRequestHeaders().getFirst("X-FromAppId") != null) {
47 fromAppId = headers.getRequestHeaders().getFirst("X-FromAppId");
50 if ((headers.getRequestHeaders().getFirst("X-TransactionId") == null)
51 || headers.getRequestHeaders().getFirst("X-TransactionId").isEmpty()) {
52 transId = java.util.UUID.randomUUID().toString();
54 transId = headers.getRequestHeaders().getFirst("X-TransactionId");
57 MdcContext.initialize(transId, CrudServiceConstants.CRD_SERVICE_NAME, "", fromAppId, fromIp);
61 * Logs the rest request.
63 public static void logRestRequest(Logger logger, Logger auditLogger, HttpServletRequest req, Response response) {
64 String respStatusString = "";
65 if (Response.Status.fromStatusCode(response.getStatus()) != null) {
66 respStatusString = Response.Status.fromStatusCode(response.getStatus()).toString();
70 logger.info(CrudServiceMsgs.PROCESS_REST_REQUEST, req.getMethod(), req.getRequestURL().toString(),
71 req.getRemoteHost(), Integer.toString(response.getStatus()));
73 // Generate audit log.
74 auditLogger.info(CrudServiceMsgs.PROCESS_REST_REQUEST,
75 new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatus())
76 .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, respStatusString),
77 (req != null) ? req.getMethod() : "Unknown", (req != null) ? req.getRequestURL().toString() : "Unknown",
78 (req != null) ? req.getRemoteHost() : "Unknown", Integer.toString(response.getStatus()) + " payload: "
79 + (response.getEntity() == null ? "" : response.getEntity().toString()));