2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.logging;
27 import org.onap.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;
33 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
34 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
35 import java.text.DateFormat;
36 import java.text.SimpleDateFormat;
37 import java.time.Instant;
38 import java.time.temporal.ChronoUnit;
39 import java.util.Date;
40 import java.util.TimeZone;
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 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, code);
128 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, "100".equals(code) || "400".equals(code)
129 ? LoggingConstants.StatusCodes.COMPLETE : LoggingConstants.StatusCodes.ERROR);
130 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
131 responseDescription != null ? responseDescription : "");
132 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
135 private static void cleanAuditErrorContext() {
137 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
138 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
139 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
140 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
143 private static void populateErrorLogContext(String errorCode, String errorDescription,
144 String targetEntity, String targetServiceName, String className) {
145 populateErrorContext(errorCode, errorDescription);
146 populateTargetContext(targetEntity, targetServiceName != null ? targetServiceName : "");
147 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
150 private static void cleanErrorLogContext() {
152 cleanTargetContext();
153 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
156 private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp,
157 String targetEntity, String targetServiceName, String statusCode, String responseCode,
158 String responseDescription, String className) {
159 populateTimeContext(beginTimeStamp, endTimeStamp);
160 populateTargetContext(targetEntity, targetServiceName);
161 populateResponseContext(statusCode, responseCode, responseDescription);
162 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className != null ? className : "");
165 private static void cleanMetricContext() {
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 populateTimeContext(Instant beginTimeStamp, Instant endTimeStamp) {
184 String beginTime = "";
186 String elapsedTime = "";
188 if (beginTimeStamp != null && endTimeStamp != null) {
189 elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp, endTimeStamp));
190 beginTime = generateTimestampStr(beginTimeStamp);
191 endTime = generateTimestampStr(endTimeStamp);
194 MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
195 MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
196 MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
199 private static String generateTimestampStr(Instant timeStamp) {
200 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
201 TimeZone tz = TimeZone.getTimeZone("UTC");
203 return df.format(Date.from(timeStamp));
206 private static void cleanTimeContext() {
207 MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
208 MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
209 MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
212 private static void populateResponseContext(String statusCode, String responseCode,
213 String responseDescription) {
214 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode != null ? statusCode : "");
215 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
216 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION,
217 responseDescription != null ? responseDescription : "");
220 private static void cleanResponseContext() {
221 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
222 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
223 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
226 private static void populateErrorContext(String errorCode, String errorDescription) {
227 MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, errorCode);
228 MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, errorDescription);
231 private static void cleanErrorContext() {
232 MDC.remove(LoggingConstants.MDCKeys.ERROR_CODE);
233 MDC.remove(LoggingConstants.MDCKeys.ERROR_DESCRIPTION);