c186a4afac15581830c5cd2beac9e4e677854d41
[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 static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
25 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
26
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.time.Instant;
30 import java.time.temporal.ChronoUnit;
31 import java.util.Date;
32 import java.util.TimeZone;
33
34 import org.openecomp.appc.i18n.Msg;
35 import org.slf4j.MDC;
36
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 import com.att.eelf.i18n.EELFResourceManager;
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 logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity, String targetServiceName, String statusCode, String responseCode, String responseDescription, String className) {
81         populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName, statusCode, responseCode, responseDescription, className);
82         metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG,
83                 MDC.get(MDC_SERVICE_NAME),
84                 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
85                 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME),
86                 MDC.get(MDC_KEY_REQUEST_ID),
87                 MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY),
88                 MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME),
89                 MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME),
90                 MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)));
91         cleanMetricContext();
92     }
93
94     private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, String className) {
95         populateTimeContext(beginTimeStamp, endTimeStamp);
96         MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, code);
97         MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, code.equals("100") || code.equals("400") ?
98                 LoggingConstants.StatusCodes.COMPLETE :
99                 LoggingConstants.StatusCodes.ERROR);
100         MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
101         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
102     }
103
104     private static void cleanAuditErrorContext() {
105         cleanTimeContext();
106         MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
107         MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
108         MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
109         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
110     }
111
112     private static void populateErrorLogContext(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String className) {
113         populateErrorContext(errorCode, errorDescription);
114         populateTargetContext(targetEntity, targetServiceName!=null?targetServiceName:"");
115         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
116     }
117
118     private static void cleanErrorLogContext() {
119         cleanErrorContext();
120         cleanTargetContext();
121         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
122     }
123
124     private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity, String targetServiceName, String statusCode, String responseCode, String responseDescription, String className) {
125         populateTimeContext(beginTimeStamp, endTimeStamp);
126         populateTargetContext(targetEntity, targetServiceName);
127         populateResponseContext(statusCode, responseCode, responseDescription);
128         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
129     }
130
131     private static void cleanMetricContext() {
132         cleanTimeContext();
133         cleanTargetContext();
134         cleanResponseContext();
135         MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
136     }
137
138     private static void populateTargetContext(String targetEntity, String targetServiceName) {
139         MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity!=null?targetEntity:"");
140         MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, targetServiceName!=null?targetServiceName:"");
141     }
142
143     private static void cleanTargetContext() {
144         MDC.remove(LoggingConstants.MDCKeys.TARGET_ENTITY);
145         MDC.remove(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME);
146     }
147
148     private static void populateTimeContext(Instant beginTimeStamp, Instant endTimeStamp) {
149         String beginTime = "";
150         String endTime = "";
151         String elapsedTime = "";
152
153         if (beginTimeStamp != null && endTimeStamp != null) {
154             elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp,  endTimeStamp));
155             beginTime = generateTimestampStr(beginTimeStamp);
156             endTime = generateTimestampStr(endTimeStamp);
157         }
158
159         MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
160         MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
161         MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
162     }
163
164     private static String generateTimestampStr(Instant timeStamp) {
165         DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
166         TimeZone tz = TimeZone.getTimeZone("UTC");
167         df.setTimeZone(tz);
168         return df.format(Date.from(timeStamp));
169     }
170
171     private static void cleanTimeContext() {
172         MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
173         MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
174         MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
175     }
176
177     private static void populateResponseContext(String statusCode, String responseCode, String responseDescription) {
178         MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode!=null?statusCode:"");
179         MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
180         MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
181     }
182
183     private static void cleanResponseContext() {
184         MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
185         MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
186         MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
187     }
188
189     private static void populateErrorContext(String errorCode, String errorDescription) {
190         MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, errorCode);
191         MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, errorDescription);
192     }
193
194     private static void cleanErrorContext() {
195         MDC.remove(LoggingConstants.MDCKeys.ERROR_CODE);
196         MDC.remove(LoggingConstants.MDCKeys.ERROR_DESCRIPTION);
197     }
198
199 }