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;
32 import org.springframework.http.HttpStatus;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
37 public class EPLogUtil {
39 // This class has no logger of its own; it uses loggers passed to it.
40 private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
43 * Formats and writes a message to the error log with the class name and the
44 * specified parameters, using log level info, warn or error appropriate for
45 * the specified severity
48 * Logger for the class where the error occurred; the logger
49 * carries the class name.
50 * @param epMessageEnum
51 * Enum carrying alarm and error severity
53 * Values used to build the message.
55 public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, String... param) {
56 logEcompError(classLogger, epMessageEnum, null, param);
60 * Formats and writes a message to the error log with the class name and the
61 * specified parameters, using log level info, warn or error appropriate for
62 * the specified severity
65 * Logger for the class where the error occurred; the logger
66 * carries the class name.
67 * @param epMessageEnum
68 * Enum carrying alarm and error severity
70 * Values used to build the message.
72 public static void logEcompError(EPAppMessagesEnum epMessageEnum, String... param) {
74 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
75 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
77 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.name());
78 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
79 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
80 MDC.put("ClassName", EPLogUtil.class.getName());
82 String resolution = EPLogUtil
83 .formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(), (Object[]) param);
84 if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
85 errorLogger.warn(resolution);
86 } else if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
87 errorLogger.info(resolution);
89 errorLogger.error(resolution);
91 } catch (Exception e) {
92 errorLogger.error("Failed to log the error code. Details: " + UserUtils.getStackTrace(e));
94 MDC.remove("ErrorCode");
95 MDC.remove("ErrorDescription");
96 MDC.remove("ClassName");
97 MDC.remove(MDC_ALERT_SEVERITY);
102 * Formats and writes a message to the error log with the class name,
103 * throwable and the specified parameters, using log level info, warn or
104 * error appropriate for the specified severity
107 * Logger for the class where the error occurred; the logger
108 * carries the class name.
109 * @param epMessageEnum
110 * Enum carrying alarm and error severity
112 * Throwable; ignored if null
114 * Array of Strings used to build the message.
116 @SuppressWarnings("static-access")
117 public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, Throwable th,
120 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
121 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
123 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.name());
124 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
125 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
127 final String message = EPLogUtil.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(),
129 if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
131 classLogger.info(classLogger.errorLogger, message);
133 classLogger.info(classLogger.errorLogger, message, th);
134 } else if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
136 classLogger.warn(classLogger.errorLogger, message);
138 classLogger.warn(classLogger.errorLogger, message, th);
141 classLogger.error(classLogger.errorLogger, message);
143 classLogger.error(classLogger.errorLogger, message, th);
147 MDC.remove(MDC_ALERT_SEVERITY);
148 MDC.remove("ErrorCode");
149 MDC.remove("ErrorDescription");
153 * Builds a string using the format and parameters.
159 private static String formatMessage(String message, Object... args) {
160 StringBuilder sbFormattedMessage = new StringBuilder();
161 if (args != null && args.length > 0 && message != null && message != "") {
162 MessageFormat mf = new MessageFormat(message);
163 sbFormattedMessage.append(mf.format(args));
165 sbFormattedMessage.append(message);
167 return sbFormattedMessage.toString();
171 * Builds a comma-separated string of values to document a user action.
183 * @return Value suitable for writing to the audit log file.
185 public static String formatAuditLogMessage(String action, String activity, String userId, String affectedId,
187 StringBuilder auditLogMsg = new StringBuilder();
188 auditLogMsg.append("Click_A:[");
189 if (action != null && !action.equals("")) {
190 auditLogMsg.append(" Action: ");
191 auditLogMsg.append(action);
194 if (activity != null && !activity.equals("")) {
195 auditLogMsg.append(",Activity CD: ");
196 auditLogMsg.append(activity);
199 if (userId != null && !userId.equals("")) {
200 auditLogMsg.append(",User ID: ");
201 auditLogMsg.append(userId);
204 if (affectedId != null && !affectedId.equals("")) {
205 auditLogMsg.append(",Affected ID: ");
206 auditLogMsg.append(affectedId);
209 if (comment != null && !comment.equals("")) {
210 auditLogMsg.append(",Comment: ");
211 auditLogMsg.append(comment);
213 auditLogMsg.append("]");
214 return auditLogMsg.toString();
218 * Builds a comma-separated string of values to document a user browser
237 * @return String value suitable for writing to the audit log file.
239 public static String formatStoreAnalyticsAuditLogMessage(String orgUserId, String appName, String action,
240 String activity, String actionLink, String page, String function, String type) {
241 StringBuilder auditLogStoreAnalyticsMsg = new StringBuilder();
242 auditLogStoreAnalyticsMsg.append("Click_Analytics:[");
243 if (orgUserId != null && !orgUserId.equals("")) {
244 auditLogStoreAnalyticsMsg.append(" Organization User ID: ");
245 auditLogStoreAnalyticsMsg.append(orgUserId);
248 if (appName != null && !appName.equals("")) {
249 auditLogStoreAnalyticsMsg.append(",AppName: ");
250 auditLogStoreAnalyticsMsg.append(appName);
253 if (action != null && !action.equals("")) {
254 auditLogStoreAnalyticsMsg.append(",Action: ");
255 auditLogStoreAnalyticsMsg.append(action);
258 if (activity != null && !activity.equals("")) {
259 auditLogStoreAnalyticsMsg.append(",Activity: ");
260 auditLogStoreAnalyticsMsg.append(activity);
263 if (actionLink != null && !actionLink.equals("")) {
264 auditLogStoreAnalyticsMsg.append(",ActionLink: ");
265 auditLogStoreAnalyticsMsg.append(actionLink);
268 if (page != null && !page.equals("")) {
269 auditLogStoreAnalyticsMsg.append(",Page: ");
270 auditLogStoreAnalyticsMsg.append(page);
273 if (function != null && !function.equals("")) {
274 auditLogStoreAnalyticsMsg.append(",Function: ");
275 auditLogStoreAnalyticsMsg.append(function);
278 if (type != null && !type.equals("")) {
279 auditLogStoreAnalyticsMsg.append(",Type: ");
280 auditLogStoreAnalyticsMsg.append(type);
282 auditLogStoreAnalyticsMsg.append("]");
283 return auditLogStoreAnalyticsMsg.toString();
286 public static void logExternalAuthAccessAlarm(EELFLoggerDelegate logger, HttpStatus res) {
287 if (res.equals(HttpStatus.UNAUTHORIZED) || res.equals(HttpStatus.FORBIDDEN)) {
288 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessAuthenticationError);
289 } else if (res.equals(HttpStatus.NOT_FOUND) || res.equals(HttpStatus.NOT_ACCEPTABLE)
290 || res.equals(HttpStatus.CONFLICT) || res.equals(HttpStatus.BAD_REQUEST)) {
291 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessConnectionError);
292 } else if (!res.equals(HttpStatus.ACCEPTED) && !res.equals(HttpStatus.OK)) {
293 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.ExternalAuthAccessGeneralError);