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