2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.portal.logging.logic;
22 import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY;
24 import java.text.MessageFormat;
26 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
27 import org.openecomp.portalsdk.core.logging.format.AlarmSeverityEnum;
28 import org.openecomp.portalsdk.core.logging.format.ErrorSeverityEnum;
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.openecomp.portalsdk.core.web.support.UserUtils;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
36 public class EPLogUtil {
38 // This class has no logger of its own; it uses loggers passed to it.
39 private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
42 * Formats and writes a message to the error log with the class name and the
43 * specified parameters, using log level info, warn or error appropriate for
44 * the specified severity
47 * Logger for the class where the error occurred; the logger
48 * carries the class name.
49 * @param epMessageEnum
50 * Enum carrying alarm and error severity
52 * Values used to build the message.
54 public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, String... param) {
55 logEcompError(classLogger, epMessageEnum, null, param);
59 * Formats and writes a message to the error log with the class name and the
60 * specified parameters, using log level info, warn or error appropriate for
61 * the specified severity
64 * Logger for the class where the error occurred; the logger
65 * carries the class name.
66 * @param epMessageEnum
67 * Enum carrying alarm and error severity
69 * Values used to build the message.
71 public static void logEcompError(EPAppMessagesEnum epMessageEnum, String... param) {
73 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
74 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
76 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.name());
77 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
78 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
79 MDC.put("ClassName", EPLogUtil.class.getName());
81 String resolution = EPLogUtil
82 .formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(), (Object[]) param);
83 if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
84 errorLogger.warn(resolution);
85 } else if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
86 errorLogger.info(resolution);
88 errorLogger.error(resolution);
90 } catch (Exception e) {
91 errorLogger.error("Failed to log the error code. Details: " + UserUtils.getStackTrace(e));
93 MDC.remove("ErrorCode");
94 MDC.remove("ErrorDescription");
95 MDC.remove("ClassName");
96 MDC.remove(MDC_ALERT_SEVERITY);
101 * Formats and writes a message to the error log with the class name,
102 * throwable and the specified parameters, using log level info, warn or
103 * error appropriate for the specified severity
106 * Logger for the class where the error occurred; the logger
107 * carries the class name.
108 * @param epMessageEnum
109 * Enum carrying alarm and error severity
111 * Throwable; ignored if null
113 * Array of Strings used to build the message.
115 @SuppressWarnings("static-access")
116 public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, Throwable th,
119 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
120 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
122 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.name());
123 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
124 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
126 final String message = EPLogUtil.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(),
128 if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
130 classLogger.info(classLogger.errorLogger, message);
132 classLogger.info(classLogger.errorLogger, message, th);
133 } else if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
135 classLogger.warn(classLogger.errorLogger, message);
137 classLogger.warn(classLogger.errorLogger, message, th);
140 classLogger.error(classLogger.errorLogger, message);
142 classLogger.error(classLogger.errorLogger, message, th);
146 MDC.remove(MDC_ALERT_SEVERITY);
147 MDC.remove("ErrorCode");
148 MDC.remove("ErrorDescription");
152 * Builds a string using the format and parameters.
158 private static String formatMessage(String message, Object... args) {
159 StringBuilder sbFormattedMessage = new StringBuilder();
160 if (args != null && args.length > 0 && message != null && message != "") {
161 MessageFormat mf = new MessageFormat(message);
162 sbFormattedMessage.append(mf.format(args));
164 sbFormattedMessage.append(message);
166 return sbFormattedMessage.toString();
170 * Builds a comma-separated string of values to document a user action.
182 * @return Value suitable for writing to the audit log file.
184 public static String formatAuditLogMessage(String action, String activity, String userId, String affectedId,
186 StringBuilder auditLogMsg = new StringBuilder();
187 auditLogMsg.append("Click_A:[");
188 if (action != null && !action.equals("")) {
189 auditLogMsg.append(" Action: ");
190 auditLogMsg.append(action);
193 if (activity != null && !activity.equals("")) {
194 auditLogMsg.append(",Activity CD: ");
195 auditLogMsg.append(activity);
198 if (userId != null && !userId.equals("")) {
199 auditLogMsg.append(",User ID: ");
200 auditLogMsg.append(userId);
203 if (affectedId != null && !affectedId.equals("")) {
204 auditLogMsg.append(",Affected ID: ");
205 auditLogMsg.append(affectedId);
208 if (comment != null && !comment.equals("")) {
209 auditLogMsg.append(",Comment: ");
210 auditLogMsg.append(comment);
212 auditLogMsg.append("]");
213 return auditLogMsg.toString();
217 * Builds a comma-separated string of values to document a user browser
236 * @return String value suitable for writing to the audit log file.
238 public static String formatStoreAnalyticsAuditLogMessage(String orgUserId, String appName, String action,
239 String activity, String actionLink, String page, String function, String type) {
240 StringBuilder auditLogStoreAnalyticsMsg = new StringBuilder();
241 auditLogStoreAnalyticsMsg.append("Click_Analytics:[");
242 if (orgUserId != null && !orgUserId.equals("")) {
243 auditLogStoreAnalyticsMsg.append(" Organization User ID: ");
244 auditLogStoreAnalyticsMsg.append(orgUserId);
247 if (appName != null && !appName.equals("")) {
248 auditLogStoreAnalyticsMsg.append(",AppName: ");
249 auditLogStoreAnalyticsMsg.append(appName);
252 if (action != null && !action.equals("")) {
253 auditLogStoreAnalyticsMsg.append(",Action: ");
254 auditLogStoreAnalyticsMsg.append(action);
257 if (activity != null && !activity.equals("")) {
258 auditLogStoreAnalyticsMsg.append(",Activity: ");
259 auditLogStoreAnalyticsMsg.append(activity);
262 if (actionLink != null && !actionLink.equals("")) {
263 auditLogStoreAnalyticsMsg.append(",ActionLink: ");
264 auditLogStoreAnalyticsMsg.append(actionLink);
267 if (page != null && !page.equals("")) {
268 auditLogStoreAnalyticsMsg.append(",Page: ");
269 auditLogStoreAnalyticsMsg.append(page);
272 if (function != null && !function.equals("")) {
273 auditLogStoreAnalyticsMsg.append(",Function: ");
274 auditLogStoreAnalyticsMsg.append(function);
277 if (type != null && !type.equals("")) {
278 auditLogStoreAnalyticsMsg.append(",Type: ");
279 auditLogStoreAnalyticsMsg.append(type);
281 auditLogStoreAnalyticsMsg.append("]");
282 return auditLogStoreAnalyticsMsg.toString();