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