2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.logging;
24 import org.openecomp.appc.i18n.Msg;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import com.att.eelf.i18n.EELFResolvableErrorEnum;
28 import com.att.eelf.i18n.EELFResourceManager;
31 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
32 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;
43 public class LoggingUtils {
45 private final static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
46 private final static EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
47 private final static EELFLogger metricLogger = EELFManager.getInstance().getMetricsLogger();
49 public static void logErrorMessage(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String additionalMessage, String className) {
50 logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage, className);
53 public static void logErrorMessage(String targetEntity, String targetServiceName, String additionalMessage, String className) {
54 logError("", "", targetEntity, targetServiceName, additionalMessage, className);
57 public static void logErrorMessage(String targetServiceName, String additionalMessage, String className) {
58 logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName, additionalMessage, className);
61 private static void logError(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String additionalMessage, String className) {
62 populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName, className);
63 errorLogger.error(additionalMessage == null ? "" : additionalMessage);
64 cleanErrorLogContext();
67 public static void logAuditMessage(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, String className) {
68 populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
69 auditLogger.info(EELFResourceManager.format(Msg.APPC_AUDIT_MSG,
70 MDC.get(MDC_SERVICE_NAME),
71 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
72 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME),
73 MDC.get(MDC_KEY_REQUEST_ID),
74 MDC.get(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP),
75 MDC.get(LoggingConstants.MDCKeys.END_TIMESTAMP),
76 MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)));
77 cleanAuditErrorContext();
80 public static void auditInfo(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, String className,EELFResolvableErrorEnum resourceId, String... arguments) {
81 populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
82 auditLogger.info(resourceId,arguments);
83 cleanAuditErrorContext();
86 public static void auditWarn(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, String className,EELFResolvableErrorEnum resourceId, String... arguments) {
87 populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
88 auditLogger.warn(resourceId,arguments);
89 cleanAuditErrorContext();
94 public static void logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity, String targetServiceName, String statusCode, String responseCode, String responseDescription, String className) {
95 populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName, statusCode, responseCode, responseDescription, className);
96 metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG,
97 MDC.get(MDC_SERVICE_NAME),
98 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
99 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME),
100 MDC.get(MDC_KEY_REQUEST_ID),
101 MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY),
102 MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME),
103 MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME),
104 MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)));
105 cleanMetricContext();
108 private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, String className) {
109 populateTimeContext(beginTimeStamp, endTimeStamp);
110 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, code);
111 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, code.equals("100") || code.equals("400") ?
112 LoggingConstants.StatusCodes.COMPLETE :
113 LoggingConstants.StatusCodes.ERROR);
114 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
115 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
118 private static void cleanAuditErrorContext() {
120 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
121 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
122 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
123 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
126 private static void populateErrorLogContext(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String className) {
127 populateErrorContext(errorCode, errorDescription);
128 populateTargetContext(targetEntity, targetServiceName!=null?targetServiceName:"");
129 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
132 private static void cleanErrorLogContext() {
134 cleanTargetContext();
135 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
138 private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity, String targetServiceName, String statusCode, String responseCode, String responseDescription, String className) {
139 populateTimeContext(beginTimeStamp, endTimeStamp);
140 populateTargetContext(targetEntity, targetServiceName);
141 populateResponseContext(statusCode, responseCode, responseDescription);
142 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
145 private static void cleanMetricContext() {
147 cleanTargetContext();
148 cleanResponseContext();
149 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
152 private static void populateTargetContext(String targetEntity, String targetServiceName) {
153 MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity!=null?targetEntity:"");
154 MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, targetServiceName!=null?targetServiceName:"");
157 private static void cleanTargetContext() {
158 MDC.remove(LoggingConstants.MDCKeys.TARGET_ENTITY);
159 MDC.remove(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME);
162 private static void populateTimeContext(Instant beginTimeStamp, Instant endTimeStamp) {
163 String beginTime = "";
165 String elapsedTime = "";
167 if (beginTimeStamp != null && endTimeStamp != null) {
168 elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp, endTimeStamp));
169 beginTime = generateTimestampStr(beginTimeStamp);
170 endTime = generateTimestampStr(endTimeStamp);
173 MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
174 MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
175 MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
178 private static String generateTimestampStr(Instant timeStamp) {
179 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
180 TimeZone tz = TimeZone.getTimeZone("UTC");
182 return df.format(Date.from(timeStamp));
185 private static void cleanTimeContext() {
186 MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
187 MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
188 MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
191 private static void populateResponseContext(String statusCode, String responseCode, String responseDescription) {
192 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode!=null?statusCode:"");
193 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
194 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
197 private static void cleanResponseContext() {
198 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
199 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
200 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
203 private static void populateErrorContext(String errorCode, String errorDescription) {
204 MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, errorCode);
205 MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, errorDescription);
208 private static void cleanErrorContext() {
209 MDC.remove(LoggingConstants.MDCKeys.ERROR_CODE);
210 MDC.remove(LoggingConstants.MDCKeys.ERROR_DESCRIPTION);