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