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 static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
25 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.time.Instant;
30 import java.time.temporal.ChronoUnit;
31 import java.util.Date;
32 import java.util.TimeZone;
34 import org.openecomp.appc.i18n.Msg;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 import com.att.eelf.i18n.EELFResourceManager;
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 logMetricsMessage(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity, String targetServiceName, String statusCode, String responseCode, String responseDescription, String className) {
81 populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName, statusCode, responseCode, responseDescription, className);
82 metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_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.TARGET_ENTITY),
88 MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME),
89 MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME),
90 MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)));
94 private static void populateAuditLogContext(Instant beginTimeStamp, Instant endTimeStamp, String code, String responseDescription, String className) {
95 populateTimeContext(beginTimeStamp, endTimeStamp);
96 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, code);
97 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, code.equals("100") || code.equals("400") ?
98 LoggingConstants.StatusCodes.COMPLETE :
99 LoggingConstants.StatusCodes.ERROR);
100 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
101 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
104 private static void cleanAuditErrorContext() {
106 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
107 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
108 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
109 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
112 private static void populateErrorLogContext(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String className) {
113 populateErrorContext(errorCode, errorDescription);
114 populateTargetContext(targetEntity, targetServiceName!=null?targetServiceName:"");
115 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
118 private static void cleanErrorLogContext() {
120 cleanTargetContext();
121 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
124 private static void populateMetricLogContext(Instant beginTimeStamp, Instant endTimeStamp, String targetEntity, String targetServiceName, String statusCode, String responseCode, String responseDescription, String className) {
125 populateTimeContext(beginTimeStamp, endTimeStamp);
126 populateTargetContext(targetEntity, targetServiceName);
127 populateResponseContext(statusCode, responseCode, responseDescription);
128 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
131 private static void cleanMetricContext() {
133 cleanTargetContext();
134 cleanResponseContext();
135 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
138 private static void populateTargetContext(String targetEntity, String targetServiceName) {
139 MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity!=null?targetEntity:"");
140 MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, targetServiceName!=null?targetServiceName:"");
143 private static void cleanTargetContext() {
144 MDC.remove(LoggingConstants.MDCKeys.TARGET_ENTITY);
145 MDC.remove(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME);
148 private static void populateTimeContext(Instant beginTimeStamp, Instant endTimeStamp) {
149 String beginTime = "";
151 String elapsedTime = "";
153 if (beginTimeStamp != null && endTimeStamp != null) {
154 elapsedTime = String.valueOf(ChronoUnit.MILLIS.between(beginTimeStamp, endTimeStamp));
155 beginTime = generateTimestampStr(beginTimeStamp);
156 endTime = generateTimestampStr(endTimeStamp);
159 MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
160 MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
161 MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
164 private static String generateTimestampStr(Instant timeStamp) {
165 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
166 TimeZone tz = TimeZone.getTimeZone("UTC");
168 return df.format(Date.from(timeStamp));
171 private static void cleanTimeContext() {
172 MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
173 MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
174 MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
177 private static void populateResponseContext(String statusCode, String responseCode, String responseDescription) {
178 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode!=null?statusCode:"");
179 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
180 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
183 private static void cleanResponseContext() {
184 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
185 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
186 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
189 private static void populateErrorContext(String errorCode, String errorDescription) {
190 MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, errorCode);
191 MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, errorDescription);
194 private static void cleanErrorContext() {
195 MDC.remove(LoggingConstants.MDCKeys.ERROR_CODE);
196 MDC.remove(LoggingConstants.MDCKeys.ERROR_DESCRIPTION);