Enhancements for the aai-common library
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / aai / logging / LoggingContext.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.logging;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.slf4j.MDC;
26
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.concurrent.TimeUnit;
30
31 public class LoggingContext {
32
33     private static final Logger logger = LoggerFactory.getLogger(LoggingContext.class);
34
35     // Response codes from Logging Guidelines
36     public static final String SUCCESS = "0";
37     public static final String PERMISSION_ERROR = "100";
38     public static final String AVAILABILITY_TIMEOUT_ERROR = "200";
39     public static final String DATA_ERROR = "300";
40     public static final String SCHEMA_ERROR = "400";
41     public static final String BUSINESS_PROCESS_ERROR = "500";
42     public static final String UNKNOWN_ERROR = "900";
43
44     public static final Map<String, String> responseMap = new HashMap();
45
46
47     // Specific Log Event Fields
48     public static enum LoggingField {
49         START_TIME("startTime"), REQUEST_ID("requestId"), SERVICE_INSTANCE_ID("serviceInstanceId"), SERVER_NAME(
50                 "serverName"), SERVICE_NAME("serviceName"), PARTNER_NAME("partnerName"), STATUS_CODE(
51                         "statusCode"), RESPONSE_CODE("responseCode"), RESPONSE_DESCRIPTION(
52                                 "responseDescription"), INSTANCE_UUID("instanceUUID"), SEVERITY(
53                                         "severity"), SERVER_IP_ADDRESS(
54                                                 "serverIpAddress"), ELAPSED_TIME("elapsedTime"), SERVER(
55                                                         "server"), CLIENT_IP_ADDRESS("clientIpAddress"), UNUSED(
56                                                                 "unused"), PROCESS_KEY("processKey"), CUSTOM_FIELD_1(
57                                                                         "customField1"), CUSTOM_FIELD_2(
58                                                                                 "customField2"), CUSTOM_FIELD_3(
59                                                                                         "customField3"), CUSTOM_FIELD_4(
60                                                                                                 "customField4"),
61
62         // Specific Metric Log Event Fields
63         TARGET_ENTITY("targetEntity"), TARGET_SERVICE_NAME("targetServiceName"),
64         // A&AI Specific Log Event Fields
65         COMPONENT("component"), STOP_WATCH_START("stopWatchStart");
66
67         private final String text;
68
69         private LoggingField(final String text) {
70             this.text = text;
71         }
72
73         public String toString() {
74             return text;
75         }
76     }
77
78     public static void init() {
79         LoggingContext.clear();
80
81     }
82
83     public static void elapsedTime(long elapsedTime, TimeUnit timeUnit) {
84         MDC.put(LoggingField.ELAPSED_TIME.toString(),
85                 String.valueOf(TimeUnit.MILLISECONDS.convert(elapsedTime, timeUnit)));
86     }
87
88     public static boolean isStopWatchStarted() {
89         final String rawStopWatchStart = MDC.get(LoggingField.STOP_WATCH_START.toString());
90         if (rawStopWatchStart == null) {
91             return false;
92         }
93         return true;
94     }
95
96     public static void stopWatchStart() {
97         MDC.put(LoggingField.STOP_WATCH_START.toString(), String.valueOf(System.nanoTime()));
98     }
99
100     public static double stopWatchStop() {
101         final long stopWatchEnd = System.nanoTime();
102         final String rawStopWatchStart = MDC.get(LoggingField.STOP_WATCH_START.toString());
103
104         if (rawStopWatchStart == null)
105             throw new StopWatchNotStartedException();
106
107         final Long stopWatchStart = Long.valueOf(rawStopWatchStart);
108
109         MDC.remove(LoggingField.STOP_WATCH_START.toString());
110
111         final double elapsedTimeMillis = (stopWatchEnd - stopWatchStart) / 1000.0 / 1000.0;
112
113         LoggingContext.elapsedTime((long) elapsedTimeMillis, TimeUnit.MILLISECONDS);
114
115         return elapsedTimeMillis;
116     }
117
118     public static void put(String key, String value) {
119         MDC.put(key, value);
120     }
121
122     public static void clear() {
123         MDC.clear();
124     }
125
126     public static void remove(String key) {
127         MDC.remove(key);
128     }
129
130 }