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