Enhancements for the aai-common library
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / aai / aailog / logs / AaiScheduledTaskAuditLog.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.logging
4  * ================================================================================
5  * Copyright © 2018 Amdocs
6  * All rights reserved.
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
11  *
12  *    http://www.apache.org/licenses/LICENSE-2.0
13  *
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  */
21 package org.onap.aai.aailog.logs;
22
23 import org.onap.logging.filter.base.MDCSetup;
24 import org.onap.logging.ref.slf4j.ONAPLogConstants;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.slf4j.MDC;
28 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
29 import org.springframework.context.annotation.Scope;
30 import org.springframework.stereotype.Component;
31
32 import javax.ws.rs.core.Response;
33 import java.util.UUID;
34
35 @Component
36 @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
37 public class AaiScheduledTaskAuditLog extends MDCSetup {
38     protected static Logger logger = LoggerFactory.getLogger(AaiScheduledTaskAuditLog.class);
39
40     public void logBefore(String serviceName, String partnerName) {
41         try {
42             String requestId = UUID.randomUUID().toString();
43             MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
44             setInvocationIdFromMDC();
45             MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, serviceName);
46             MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
47             setServerFQDN();
48             setClientIPAddress(null);
49             setInstanceID();
50             setEntryTimeStamp();
51             MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
52             setLogTimestamp();
53             setElapsedTime();
54             logger.info(ONAPLogConstants.Markers.ENTRY, "Entering");
55         } catch (Exception e) {
56             logger.warn("Error in AaiScheduledTaskAuditLog logBefore", e.getMessage());
57         }
58
59     }
60
61     public void logAfter() {
62         try {
63             // TODO: how do we know if there was an error
64             setResponseStatusCode(Response.Status.OK.getStatusCode());
65             setLogTimestamp();
66             setElapsedTime();
67             logger.info(ONAPLogConstants.Markers.EXIT, "Exiting.");
68         } catch (Exception e) {
69             logger.warn("Error in AaiScheduledTaskAuditLog logAfter", e.getMessage());
70         } finally {
71             MDC.clear();
72         }
73     }
74 }