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.EELFResourceManager;
30 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
31 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
33 import java.text.DateFormat;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.TimeZone;
40 public class LoggingUtils {
42 private final static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
43 private final static EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
44 private final static EELFLogger metricLogger = EELFManager.getInstance().getMetricsLogger();
46 public static void logErrorMessage(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String additionalMessage, String className) {
47 logError(errorCode, errorDescription, targetEntity, targetServiceName, additionalMessage, className);
50 public static void logErrorMessage(String targetEntity, String targetServiceName, String additionalMessage, String className) {
51 logError("", "", targetEntity, targetServiceName, additionalMessage, className);
54 public static void logErrorMessage(String targetServiceName, String additionalMessage, String className) {
55 logError("", "", LoggingConstants.TargetNames.APPC, targetServiceName, additionalMessage, className);
58 private static void logError(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String additionalMessage, String className) {
59 populateErrorLogContext(errorCode, errorDescription, targetEntity, targetServiceName, className);
60 errorLogger.error(additionalMessage == null ? "" : additionalMessage);
61 cleanErrorLogContext();
64 public static void logAuditMessage(Date beginTimeStamp, Date endTimeStamp, String code, String responseDescription, String className) {
65 populateAuditLogContext(beginTimeStamp, endTimeStamp, code, responseDescription, className);
66 auditLogger.info(EELFResourceManager.format(Msg.APPC_AUDIT_MSG,
67 MDC.get(MDC_SERVICE_NAME),
68 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
69 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME),
70 MDC.get(MDC_KEY_REQUEST_ID),
71 MDC.get(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP),
72 MDC.get(LoggingConstants.MDCKeys.END_TIMESTAMP),
73 MDC.get(LoggingConstants.MDCKeys.RESPONSE_CODE)));
74 cleanAuditErrorContext();
77 public static void logMetricsMessage(Date beginTimeStamp, Date endTimeStamp, String targetEntity, String targetServiceName, String statusCode, String responseCode, String responseDescription, String className) {
78 populateMetricLogContext(beginTimeStamp, endTimeStamp, targetEntity, targetServiceName, statusCode, responseCode, responseDescription, className);
79 metricLogger.info(EELFResourceManager.format(Msg.APPC_METRIC_MSG,
80 MDC.get(MDC_SERVICE_NAME),
81 MDC.get(LoggingConstants.MDCKeys.TARGET_VIRTUAL_ENTITY),
82 MDC.get(LoggingConstants.MDCKeys.PARTNER_NAME),
83 MDC.get(MDC_KEY_REQUEST_ID),
84 MDC.get(LoggingConstants.MDCKeys.TARGET_ENTITY),
85 MDC.get(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME),
86 MDC.get(LoggingConstants.MDCKeys.ELAPSED_TIME),
87 MDC.get(LoggingConstants.MDCKeys.STATUS_CODE)));
91 private static void populateAuditLogContext(Date beginTimeStamp, Date endTimeStamp, String code, String responseDescription, String className) {
92 populateTimeContext(beginTimeStamp, endTimeStamp);
93 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, code);
94 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, code.equals("100") || code.equals("400") ?
95 LoggingConstants.StatusCodes.COMPLETE :
96 LoggingConstants.StatusCodes.ERROR);
97 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
98 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
101 private static void cleanAuditErrorContext() {
103 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
104 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
105 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
106 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
109 private static void populateErrorLogContext(String errorCode, String errorDescription, String targetEntity, String targetServiceName, String className) {
110 populateErrorContext(errorCode, errorDescription);
111 populateTargetContext(targetEntity, targetServiceName!=null?targetServiceName:"");
112 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
115 private static void cleanErrorLogContext() {
117 cleanTargetContext();
118 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
121 private static void populateMetricLogContext(Date beginTimeStamp, Date endTimeStamp, String targetEntity, String targetServiceName, String statusCode, String responseCode, String responseDescription, String className) {
122 populateTimeContext(beginTimeStamp, endTimeStamp);
123 populateTargetContext(targetEntity, targetServiceName);
124 populateResponseContext(statusCode, responseCode, responseDescription);
125 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, className!=null?className:"");
128 private static void cleanMetricContext() {
130 cleanTargetContext();
131 cleanResponseContext();
132 MDC.remove(LoggingConstants.MDCKeys.CLASS_NAME);
135 private static void populateTargetContext(String targetEntity, String targetServiceName) {
136 MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, targetEntity!=null?targetEntity:"");
137 MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, targetServiceName!=null?targetServiceName:"");
140 private static void cleanTargetContext() {
141 MDC.remove(LoggingConstants.MDCKeys.TARGET_ENTITY);
142 MDC.remove(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME);
145 private static void populateTimeContext(Date beginTimeStamp, Date endTimeStamp) {
146 String beginTime = "";
148 String elapsedTime = "";
150 if (beginTimeStamp != null && endTimeStamp != null) {
151 elapsedTime = String.valueOf(endTimeStamp.getTime() - beginTimeStamp.getTime());
152 beginTime = generateTimestampStr(beginTimeStamp);
153 endTime = generateTimestampStr(endTimeStamp);
156 MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, beginTime);
157 MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, endTime);
158 MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, elapsedTime);
161 private static String generateTimestampStr(Date timeStamp) {
162 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
163 TimeZone tz = TimeZone.getTimeZone("UTC");
165 return df.format(timeStamp);
168 private static void cleanTimeContext() {
169 MDC.remove(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP);
170 MDC.remove(LoggingConstants.MDCKeys.END_TIMESTAMP);
171 MDC.remove(LoggingConstants.MDCKeys.ELAPSED_TIME);
174 private static void populateResponseContext(String statusCode, String responseCode, String responseDescription) {
175 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, statusCode!=null?statusCode:"");
176 MDC.put(LoggingConstants.MDCKeys.RESPONSE_CODE, responseCode);
177 MDC.put(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION, responseDescription!=null?responseDescription:"");
180 private static void cleanResponseContext() {
181 MDC.remove(LoggingConstants.MDCKeys.STATUS_CODE);
182 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_CODE);
183 MDC.remove(LoggingConstants.MDCKeys.RESPONSE_DESCRIPTION);
186 private static void populateErrorContext(String errorCode, String errorDescription) {
187 MDC.put(LoggingConstants.MDCKeys.ERROR_CODE, errorCode);
188 MDC.put(LoggingConstants.MDCKeys.ERROR_DESCRIPTION, errorDescription);
191 private static void cleanErrorContext() {
192 MDC.remove(LoggingConstants.MDCKeys.ERROR_CODE);
193 MDC.remove(LoggingConstants.MDCKeys.ERROR_DESCRIPTION);