2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.crud.logging;
23 import org.onap.aai.cl.api.LogFields;
24 import org.onap.aai.cl.api.LogLine;
25 import org.onap.aai.cl.api.Logger;
26 import org.onap.aai.cl.mdc.MdcContext;
27 import org.onap.crud.util.CrudServiceConstants;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.core.HttpHeaders;
32 import javax.ws.rs.core.Response;
34 public class LoggingUtil {
36 * Initializes mdc context.
38 public static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) {
39 String fromIp = httpReq.getRemoteAddr();
41 MdcContext.initialize(getTransactionId(headers), CrudServiceConstants.CRD_SERVICE_NAME, "", getAppId(headers), fromIp);
44 public static String getAppId(HttpHeaders headers) {
45 String fromAppId = "";
46 if (headers.getRequestHeaders().getFirst("X-FromMsId") != null) {
47 fromAppId = headers.getRequestHeaders().getFirst("X-FromMsId");
48 } else if (headers.getRequestHeaders().getFirst("X-FromAppId") != null) {
49 fromAppId = headers.getRequestHeaders().getFirst("X-FromAppId");
54 public static String getTransactionId(HttpHeaders headers) {
55 String transId = null;
56 if ((headers.getRequestHeaders().getFirst("X-TransactionId") == null)
57 || headers.getRequestHeaders().getFirst("X-TransactionId").isEmpty()) {
58 transId = java.util.UUID.randomUUID().toString();
60 transId = headers.getRequestHeaders().getFirst("X-TransactionId");
66 * Logs the rest request.
68 public static void logRestRequest(Logger logger, Logger auditLogger, HttpServletRequest req, Response response) {
69 String respStatusString = "";
70 if (Response.Status.fromStatusCode(response.getStatus()) != null) {
71 respStatusString = Response.Status.fromStatusCode(response.getStatus()).toString();
75 logger.info(CrudServiceMsgs.PROCESS_REST_REQUEST, req.getMethod(), req.getRequestURL().toString(),
76 req.getRemoteHost(), Integer.toString(response.getStatus()));
78 // Generate audit log.
79 auditLogger.info(CrudServiceMsgs.PROCESS_REST_REQUEST,
80 new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatus())
81 .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, respStatusString),
82 req.getMethod(), req.getRequestURL().toString(),
83 req.getRemoteHost(), Integer.toString(response.getStatus()) + " payload: "
84 + (response.getEntity() == null ? "" : response.getEntity().toString()));