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;
44 public class LoggingUtils {
46 private static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
47 private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
48 private static final EELFLogger metricLogger = EELFManager.getInstance().getMetricsLogger();
50 private LoggingUtils() {
51 throw new IllegalAccessError("LoggingUtils");
54 public static void logErrorMessage(String errorCode, String errorDescription,
55 String targetEntity, String targetServiceName, String additionalMessage,
57 logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage,
61 public static void logErrorMessage(String targetEntity, String targetServiceName,
62 String additionalMessage, String className) {
63 logError("", "", targetEntity, targetServiceName, additionalMessage, className);
66 public static void logErrorMessage(String targetServiceName, String additionalMessage,
68 logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName, additionalMessage,
72 private static void logError(String errorCode, String errorDescription, String targetEntity,
73 String targetServiceName, String additionalMessage, String className) {
74 populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName,
76 errorLogger.error(additionalMessage == null ? "" : additionalMessage);
77 cleanErrorLogContext();
80 public static void logAuditMessage(Instant beginTimeStamp, Instant endTimeStamp, String code,
81 String responseDescription, String className) {
82 populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
83 auditLogger.info(EELFResourceManager.format(Msg.APPC_AUDIT_MSG, MDC.get(MDC_SERVICE_NAME),
84 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
85 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME), MDC.get(MDC_KEY_REQUEST_ID),
86 MDC.get(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP),
87 MDC.get(LoggingConstants.MDCKeys.END_TIMESTAMP),
88 MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)));
89 cleanAuditErrorContext();
92 public static void auditInfo(Instant beginTimeStamp, Instant endTimeStamp, String code,
93 String responseDescription, String className, EELFResolvableErrorEnum resourceId,
94 String... arguments) {
95 populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
96 auditLogger.info(resourceId, arguments);
97 cleanAuditErrorContext();
100 public static void auditWarn(Instant beginTimeStamp, Instant endTimeStamp, String code,
101 String responseDescription, String className, EELFResolvableErrorEnum resourceId,
102 String... arguments) {
103 populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
104 auditLogger.warn(resourceId, arguments);
105 cleanAuditErrorContext();
108 public static void logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp,
109 String targetEntity, String targetServiceName, String statusCode, String responseCode,
110 String responseDescription, String className) {
111 populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName,
112 statusCode, responseCode, responseDescription, className);
113 metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG, MDC.get(MDC_SERVICE_NAME),
114 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
115 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME), MDC.get(MDC_KEY_REQUEST_ID),
116 MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY),
117 MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME),
118 MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME),
119 MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)));
120 cleanMetricContext();
123 private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp,
124 String code, String responseDescription, String className) {
125 populateTimeContext(beginTimeStamp, endTimeStamp);
126 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, code);
127 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, "100".equals(code) || "400".equals(code)
128 ? LoggingConstants.StatusCodes.COMPLETE : LoggingConstants.StatusCodes.ERROR);
129 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
130 responseDescription != null ? responseDescription : "");
131 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
134 private static void cleanAuditErrorContext() {
136 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
137 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
138 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
139 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
142 private static void populateErrorLogContext(String errorCode, String errorDescription,
143 String targetEntity, String targetServiceName, String className) {
144 populateErrorContext(errorCode, errorDescription);
145 populateTargetContext(targetEntity, targetServiceName != null ? targetServiceName : "");
146 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
149 private static void cleanErrorLogContext() {
151 cleanTargetContext();
152 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
155 private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp,
156 String targetEntity, String targetServiceName, String statusCode, String responseCode,
157 String responseDescription, String className) {
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() {
166 cleanTargetContext();
167 cleanResponseContext();
168 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
171 private static void populateTargetContext(String targetEntity, String targetServiceName) {
172 MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity != null ? targetEntity : "");
173 MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME,
174 targetServiceName != null ? targetServiceName : "");
177 private static void cleanTargetContext() {
178 MDC.remove(LoggingConstants.MDCKeys.TARGET_ENTITY);
179 MDC.remove(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME);
182 private static void populateTimeContext(Instant beginTimeStamp, Instant endTimeStamp) {
183 String beginTime = "";
185 String elapsedTime = "";
187 if (beginTimeStamp != null && endTimeStamp != null) {
188 elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp, endTimeStamp));
189 beginTime = generateTimestampStr(beginTimeStamp);
190 endTime = generateTimestampStr(endTimeStamp);
193 MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
194 MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
195 MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
198 private static String generateTimestampStr(Instant timeStamp) {
199 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
200 TimeZone tz = TimeZone.getTimeZone("UTC");
202 return df.format(Date.from(timeStamp));
205 private static void cleanTimeContext() {
206 MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
207 MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
208 MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
211 private static void populateResponseContext(String statusCode, String responseCode,
212 String responseDescription) {
213 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode != null ? statusCode : "");
214 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
215 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
216 responseDescription != null ? responseDescription : "");
219 private static void cleanResponseContext() {
220 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
221 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
222 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
225 private static void populateErrorContext(String errorCode, String errorDescription) {
226 MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, errorCode);
227 MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, errorDescription);
230 private static void cleanErrorContext() {
231 MDC.remove(LoggingConstants.MDCKeys.ERROR_CODE);
232 MDC.remove(LoggingConstants.MDCKeys.ERROR_DESCRIPTION);