Revert "remove redundant code under appc-common-bundle/java"
[appc.git] / appc-core / appc-common-bundle / java / org / onap / appc / logging / LoggingUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.logging;
25
26 import org.onap.appc.i18n.Msg;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.att.eelf.i18n.EELFResolvableErrorEnum;
30 import com.att.eelf.i18n.EELFResourceManager;
31 import org.slf4j.MDC;
32 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
33 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
34 import java.text.DateFormat;
35 import java.text.SimpleDateFormat;
36 import java.time.Instant;
37 import java.time.temporal.ChronoUnit;
38 import java.util.Date;
39 import java.util.TimeZone;
40
41 /**
42  * Logging utilities
43  */
44 public class LoggingUtils {
45
46     private static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
47     private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
48     private static final EELFLogger metricLogger = EELFManager.getInstance().getMetricsLogger();
49
50     private LoggingUtils() {
51         throw new IllegalAccessError("LoggingUtils");
52     }
53
54     public static void logErrorMessage(String errorCode, String errorDescription,
55             String targetEntity, String targetServiceName, String additionalMessage,
56             String className) {
57         logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage,
58                 className);
59     }
60
61     public static void logErrorMessage(String targetEntity, String targetServiceName,
62             String additionalMessage, String className) {
63         logError("", "", targetEntity, targetServiceName, additionalMessage, className);
64     }
65
66     public static void logErrorMessage(String targetServiceName, String additionalMessage,
67             String className) {
68         logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName, additionalMessage,
69                 className);
70     }
71
72     private static void logError(String errorCode, String errorDescription, String targetEntity,
73             String targetServiceName, String additionalMessage, String className) {
74         populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName,
75                 className);
76         errorLogger.error(additionalMessage == null ? "" : additionalMessage);
77         cleanErrorLogContext();
78     }
79
80     public static void logAuditMessage(Instant beginTimeStamp, Instant endTimeStamp, String code,
81             String responseDescription, String className) {
82         populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
83         auditLogger.info(EELFResourceManager.format(Msg.APPC_AUDIT_MSG, MDC.get(MDC_SERVICE_NAME),
84                 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
85                 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME), MDC.get(MDC_KEY_REQUEST_ID),
86                 MDC.get(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP),
87                 MDC.get(LoggingConstants.MDCKeys.END_TIMESTAMP),
88                 MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)));
89         cleanAuditErrorContext();
90     }
91
92     public static void auditInfo(Instant beginTimeStamp, Instant endTimeStamp, String code,
93             String responseDescription, String className, EELFResolvableErrorEnum resourceId,
94             String... arguments) {
95         populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
96         auditLogger.info(resourceId, arguments);
97         cleanAuditErrorContext();
98     }
99
100     public static void auditWarn(Instant beginTimeStamp, Instant endTimeStamp, String code,
101             String responseDescription, String className, EELFResolvableErrorEnum resourceId,
102             String... arguments) {
103         populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
104         auditLogger.warn(resourceId, arguments);
105         cleanAuditErrorContext();
106     }
107
108     public static void logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp,
109             String targetEntity, String targetServiceName, String statusCode, String responseCode,
110             String responseDescription, String className) {
111         populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName,
112                 statusCode, responseCode, responseDescription, className);
113         metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG, MDC.get(MDC_SERVICE_NAME),
114                 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
115                 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME), MDC.get(MDC_KEY_REQUEST_ID),
116                 MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY),
117                 MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME),
118                 MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME),
119                 MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)));
120         cleanMetricContext();
121     }
122
123     private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp,
124             String code, String responseDescription, String className) {
125         populateTimeContext(beginTimeStamp, endTimeStamp);
126         MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, code);
127         MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, "100".equals(code) || "400".equals(code)
128                 ? LoggingConstants.StatusCodes.COMPLETE : LoggingConstants.StatusCodes.ERROR);
129         MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
130                 responseDescription != null ? responseDescription : "");
131         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
132     }
133
134     private static void cleanAuditErrorContext() {
135         cleanTimeContext();
136         MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
137         MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
138         MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
139         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
140     }
141
142     private static void populateErrorLogContext(String errorCode, String errorDescription,
143             String targetEntity, String targetServiceName, String className) {
144         populateErrorContext(errorCode, errorDescription);
145         populateTargetContext(targetEntity, targetServiceName != null ? targetServiceName : "");
146         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
147     }
148
149     private static void cleanErrorLogContext() {
150         cleanErrorContext();
151         cleanTargetContext();
152         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
153     }
154
155     private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp,
156             String targetEntity, String targetServiceName, String statusCode, String responseCode,
157             String responseDescription, String className) {
158         populateTimeContext(beginTimeStamp, endTimeStamp);
159         populateTargetContext(targetEntity, targetServiceName);
160         populateResponseContext(statusCode, responseCode, responseDescription);
161         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
162     }
163
164     private static void cleanMetricContext() {
165         cleanTimeContext();
166         cleanTargetContext();
167         cleanResponseContext();
168         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
169     }
170
171     private static void populateTargetContext(String targetEntity, String targetServiceName) {
172         MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity != null ? targetEntity : "");
173         MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME,
174                 targetServiceName != null ? targetServiceName : "");
175     }
176
177     private static void cleanTargetContext() {
178         MDC.remove(LoggingConstants.MDCKeys.TARGET_ENTITY);
179         MDC.remove(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME);
180     }
181
182     private static void populateTimeContext(Instant beginTimeStamp, Instant endTimeStamp) {
183         String beginTime = "";
184         String endTime = "";
185         String elapsedTime = "";
186
187         if (beginTimeStamp != null && endTimeStamp != null) {
188             elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp, endTimeStamp));
189             beginTime = generateTimestampStr(beginTimeStamp);
190             endTime = generateTimestampStr(endTimeStamp);
191         }
192
193         MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
194         MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
195         MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
196     }
197
198     private static String generateTimestampStr(Instant timeStamp) {
199         DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
200         TimeZone tz = TimeZone.getTimeZone("UTC");
201         df.setTimeZone(tz);
202         return df.format(Date.from(timeStamp));
203     }
204
205     private static void cleanTimeContext() {
206         MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
207         MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
208         MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
209     }
210
211     private static void populateResponseContext(String statusCode, String responseCode,
212             String responseDescription) {
213         MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode != null ? statusCode : "");
214         MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
215         MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
216                 responseDescription != null ? responseDescription : "");
217     }
218
219     private static void cleanResponseContext() {
220         MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
221         MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
222         MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
223     }
224
225     private static void populateErrorContext(String errorCode, String errorDescription) {
226         MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, errorCode);
227         MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, errorDescription);
228     }
229
230     private static void cleanErrorContext() {
231         MDC.remove(LoggingConstants.MDCKeys.ERROR_CODE);
232         MDC.remove(LoggingConstants.MDCKeys.ERROR_DESCRIPTION);
233     }
234
235 }