Merge "[AAI] Fix doc config files"
[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 java.util.HashMap;
24 import java.util.Map;
25 import java.util.concurrent.TimeUnit;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.slf4j.MDC;
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     // Specific Log Event Fields
47     public static enum LoggingField {
48         START_TIME("startTime"), REQUEST_ID("requestId"), SERVICE_INSTANCE_ID("serviceInstanceId"), SERVER_NAME(
49                 "serverName"), SERVICE_NAME("serviceName"), PARTNER_NAME("partnerName"), STATUS_CODE(
50                         "statusCode"), RESPONSE_CODE("responseCode"), RESPONSE_DESCRIPTION(
51                                 "responseDescription"), INSTANCE_UUID("instanceUUID"), SEVERITY(
52                                         "severity"), SERVER_IP_ADDRESS(
53                                                 "serverIpAddress"), ELAPSED_TIME("elapsedTime"), SERVER(
54                                                         "server"), CLIENT_IP_ADDRESS("clientIpAddress"), UNUSED(
55                                                                 "unused"), PROCESS_KEY("processKey"), CUSTOM_FIELD_1(
56                                                                         "customField1"), CUSTOM_FIELD_2(
57                                                                                 "customField2"), CUSTOM_FIELD_3(
58                                                                                         "customField3"), CUSTOM_FIELD_4(
59                                                                                                 "customField4"),
60
61         // Specific Metric Log Event Fields
62         TARGET_ENTITY("targetEntity"), TARGET_SERVICE_NAME("targetServiceName"),
63         // A&AI Specific Log Event Fields
64         COMPONENT("component"), STOP_WATCH_START("stopWatchStart");
65
66         private final String text;
67
68         private LoggingField(final String text) {
69             this.text = text;
70         }
71
72         public String toString() {
73             return text;
74         }
75     }
76
77     public static void init() {
78         LoggingContext.clear();
79
80     }
81
82     public static void elapsedTime(long elapsedTime, TimeUnit timeUnit) {
83         MDC.put(LoggingField.ELAPSED_TIME.toString(),
84                 String.valueOf(TimeUnit.MILLISECONDS.convert(elapsedTime, timeUnit)));
85     }
86
87     public static boolean isStopWatchStarted() {
88         final String rawStopWatchStart = MDC.get(LoggingField.STOP_WATCH_START.toString());
89         if (rawStopWatchStart == null) {
90             return false;
91         }
92         return true;
93     }
94
95     public static void stopWatchStart() {
96         MDC.put(LoggingField.STOP_WATCH_START.toString(), String.valueOf(System.nanoTime()));
97     }
98
99     public static double stopWatchStop() {
100         final long stopWatchEnd = System.nanoTime();
101         final String rawStopWatchStart = MDC.get(LoggingField.STOP_WATCH_START.toString());
102
103         if (rawStopWatchStart == null)
104             throw new StopWatchNotStartedException();
105
106         final Long stopWatchStart = Long.valueOf(rawStopWatchStart);
107
108         MDC.remove(LoggingField.STOP_WATCH_START.toString());
109
110         final double elapsedTimeMillis = (stopWatchEnd - stopWatchStart) / 1000.0 / 1000.0;
111
112         LoggingContext.elapsedTime((long) elapsedTimeMillis, TimeUnit.MILLISECONDS);
113
114         return elapsedTimeMillis;
115     }
116
117     public static void put(String key, String value) {
118         MDC.put(key, value);
119     }
120
121     public static void clear() {
122         MDC.clear();
123     }
124
125     public static void remove(String key) {
126         MDC.remove(key);
127     }
128
129 }