2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.logging;
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;
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 import java.util.UUID;
45 public class LoggingUtils {
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();
51 private LoggingUtils() {
52 throw new IllegalAccessError("LoggingUtils");
55 public static void logErrorMessage(String errorCode, String errorDescription,
56 String targetEntity, String targetServiceName, String additionalMessage,
58 logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage,
62 public static void logErrorMessage(String targetEntity, String targetServiceName,
63 String additionalMessage, String className) {
64 logError("", "", targetEntity, targetServiceName, additionalMessage, className);
67 public static void logErrorMessage(String targetServiceName, String additionalMessage,
69 logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName, additionalMessage,
73 private static void logError(String errorCode, String errorDescription, String targetEntity,
74 String targetServiceName, String additionalMessage, String className) {
75 populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName,
77 errorLogger.error(additionalMessage == null ? "" : additionalMessage);
78 cleanErrorLogContext();
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();
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();
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();
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();
124 private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp,
125 String code, String responseDescription, String className) {
126 populateTimeContext(beginTimeStamp, endTimeStamp);
127 populateRequestContext();
128 String statusCode = ("100".equals(code) || "400".equals(code))
129 ? LoggingConstants.StatusCodes.COMPLETE : LoggingConstants.StatusCodes.ERROR;
130 populateResponseContext(statusCode, code, responseDescription );
131 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
134 private static void cleanAuditErrorContext() {
135 cleanRequestContext();
137 cleanResponseContext();
138 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
141 private static void populateErrorLogContext(String errorCode, String errorDescription,
142 String targetEntity, String targetServiceName, String className) {
143 populateErrorContext(errorCode, errorDescription);
144 populateTargetContext(targetEntity, targetServiceName != null ? targetServiceName : "");
145 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
148 private static void cleanErrorLogContext() {
150 cleanTargetContext();
151 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
154 private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp,
155 String targetEntity, String targetServiceName, String statusCode, String responseCode,
156 String responseDescription, String className) {
157 populateRequestContext();
158 populateTimeContext(beginTimeStamp, endTimeStamp);
159 populateTargetContext(targetEntity, targetServiceName);
160 populateResponseContext(statusCode, responseCode, responseDescription);
161 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
164 private static void cleanMetricContext() {
165 cleanRequestContext();
167 cleanTargetContext();
168 cleanResponseContext();
169 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
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 : "");
178 private static void cleanTargetContext() {
179 MDC.remove(LoggingConstants.MDCKeys.TARGET_ENTITY);
180 MDC.remove(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME);
183 private static void populateRequestContext() {
185 UUID.fromString(MDC.get(MDC_KEY_REQUEST_ID));
186 //reaching here without exception means existing RequestId is
187 //valid UUID as per ECOMP logging standards, no-op
188 } catch (Exception e) {
189 MDC.put(MDC_KEY_REQUEST_ID, UUID.randomUUID().toString());
193 String partnerName = MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME);
195 //ECOMP logging standards require some value for PartnerName. Default to appc if empty
196 if (partnerName.isEmpty())
197 MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, "appc");
198 } catch (Exception e) {
199 MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, "appc");
203 String serviceName = MDC.get(MDC_SERVICE_NAME);
205 //ECOMP logging standards require some value for ServiceName. Default to DEFAULT if empty
206 if (serviceName.isEmpty())
207 MDC.put(MDC_SERVICE_NAME, "DEFAULT");
208 } catch (Exception e) {
209 MDC.put(MDC_SERVICE_NAME, "DEFAULT");
213 private static void cleanRequestContext() {
214 MDC.remove(MDC_KEY_REQUEST_ID);
215 MDC.remove(LoggingConstants.MDCKeys.PARTNER_NAME);
216 MDC.remove(MDC_SERVICE_NAME);
218 private static void populateTimeContext(Instant beginTimeStamp, Instant endTimeStamp) {
219 String beginTime = "";
221 String elapsedTime = "";
223 if (beginTimeStamp != null && endTimeStamp != null) {
224 elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp, endTimeStamp));
225 beginTime = generateTimestampStr(beginTimeStamp);
226 endTime = generateTimestampStr(endTimeStamp);
229 MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
230 MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
231 MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
234 public static String generateTimestampStr(Instant timeStamp) {
235 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
236 TimeZone tz = TimeZone.getTimeZone("UTC");
238 return df.format(Date.from(timeStamp));
241 private static void cleanTimeContext() {
242 MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
243 MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
244 MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
247 private static void populateResponseContext(String statusCode, String responseCode,
248 String responseDescription) {
249 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode != null ? statusCode : "");
250 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
251 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
252 responseDescription != null ? responseDescription : "");
255 private static void cleanResponseContext() {
256 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
257 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
258 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
261 private static void populateErrorContext(String errorCode, String errorDescription) {
262 MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, errorCode);
263 MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, errorDescription);
266 private static void cleanErrorContext() {
267 MDC.remove(LoggingConstants.MDCKeys.ERROR_CODE);
268 MDC.remove(LoggingConstants.MDCKeys.ERROR_DESCRIPTION);