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.openecomp.appc.logging;
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;
34 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
35 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
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;
47 public class LoggingUtils {
49 private final static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
50 private final static EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
51 private final static EELFLogger metricLogger = EELFManager.getInstance().getMetricsLogger();
53 private LoggingUtils() {
54 throw new IllegalAccessError("LoggingUtils");
57 public static void logErrorMessage(String errorCode, String errorDescription, String targetEntity,
58 String targetServiceName, String additionalMessage, String className) {
59 logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage, className);
62 public static void logErrorMessage(String targetEntity, String targetServiceName, String additionalMessage,
64 logError("", "", targetEntity, targetServiceName, additionalMessage, className);
67 public static void logErrorMessage(String targetServiceName, String additionalMessage, String className) {
68 logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName,
69 additionalMessage, className);
72 private static void logError(String errorCode, String errorDescription, String targetEntity,
73 String targetServiceName, String additionalMessage, String className) {
74 populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName, className);
75 errorLogger.error(additionalMessage == null ? "" : additionalMessage);
76 cleanErrorLogContext();
79 public static void logAuditMessage(Instant beginTimeStamp, Instant endTimeStamp, String code,
80 String responseDescription, String className) {
81 populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
82 auditLogger.info(EELFResourceManager.format(Msg.APPC_AUDIT_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.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, String responseDescription,
94 String className,EELFResolvableErrorEnum resourceId, 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, String responseDescription,
101 String className,EELFResolvableErrorEnum resourceId, String... arguments) {
102 populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
103 auditLogger.warn(resourceId,arguments);
104 cleanAuditErrorContext();
109 public static void logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity,
110 String targetServiceName, String statusCode, String responseCode,
111 String responseDescription, String className) {
112 populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName, statusCode,
113 responseCode, responseDescription, className);
114 metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG,
115 MDC.get(MDC_SERVICE_NAME),
116 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
117 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME),
118 MDC.get(MDC_KEY_REQUEST_ID),
119 MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY),
120 MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME),
121 MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME),
122 MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)));
123 cleanMetricContext();
126 private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp, String code,
127 String responseDescription, String className) {
128 populateTimeContext(beginTimeStamp, endTimeStamp);
129 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, code);
130 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, "100".equals(code) || "400".equals(code) ?
131 LoggingConstants.StatusCodes.COMPLETE :
132 LoggingConstants.StatusCodes.ERROR);
133 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
134 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
137 private static void cleanAuditErrorContext() {
139 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
140 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
141 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
142 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
145 private static void populateErrorLogContext(String errorCode, String errorDescription, String targetEntity,
146 String targetServiceName, String className) {
147 populateErrorContext(errorCode, errorDescription);
148 populateTargetContext(targetEntity, targetServiceName!=null?targetServiceName:"");
149 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
152 private static void cleanErrorLogContext() {
154 cleanTargetContext();
155 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
158 private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity,
159 String targetServiceName, String statusCode, String responseCode,
160 String responseDescription, String className) {
161 populateTimeContext(beginTimeStamp, endTimeStamp);
162 populateTargetContext(targetEntity, targetServiceName);
163 populateResponseContext(statusCode, responseCode, responseDescription);
164 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
167 private static void cleanMetricContext() {
169 cleanTargetContext();
170 cleanResponseContext();
171 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
174 private static void populateTargetContext(String targetEntity, String targetServiceName) {
175 MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity!=null?targetEntity:"");
176 MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, targetServiceName!=null?targetServiceName:"");
179 private static void cleanTargetContext() {
180 MDC.remove(LoggingConstants.MDCKeys.TARGET_ENTITY);
181 MDC.remove(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME);
184 private static void populateTimeContext(Instant beginTimeStamp, Instant endTimeStamp) {
185 String beginTime = "";
187 String elapsedTime = "";
189 if (beginTimeStamp != null && endTimeStamp != null) {
190 elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp, endTimeStamp));
191 beginTime = generateTimestampStr(beginTimeStamp);
192 endTime = generateTimestampStr(endTimeStamp);
195 MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
196 MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
197 MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
200 private static String generateTimestampStr(Instant timeStamp) {
201 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
202 TimeZone tz = TimeZone.getTimeZone("UTC");
204 return df.format(Date.from(timeStamp));
207 private static void cleanTimeContext() {
208 MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
209 MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
210 MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
213 private static void populateResponseContext(String statusCode, String responseCode, 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, 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);