ebda67e6d4c5b8afc81cb200c699ccbde921db95
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / logging / logic / EPLogUtil.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.logging.logic;
21
22 import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY;
23
24 import java.text.MessageFormat;
25
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;
31 import org.slf4j.MDC;
32
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35
36 public class EPLogUtil {
37
38         // This class has no logger of its own; it uses loggers passed to it.
39         private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
40
41         /**
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
45          * 
46          * @param classLogger
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
51          * @param param
52          *            Values used to build the message.
53          */
54         public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, String... param) {
55                 logEcompError(classLogger, epMessageEnum, null, param);
56         }
57
58         /**
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
62          * 
63          * @param classLogger
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
68          * @param param
69          *            Values used to build the message.
70          */
71         public static void logEcompError(EPAppMessagesEnum epMessageEnum, String... param) {
72                 try {
73                         AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
74                         ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
75
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());
80
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);
87                         } else {
88                                 errorLogger.error(resolution);
89                         }
90                 } catch (Exception e) {
91                         errorLogger.error("Failed to log the error code. Details: " + UserUtils.getStackTrace(e));
92                 } finally {
93                         MDC.remove("ErrorCode");
94                         MDC.remove("ErrorDescription");
95                         MDC.remove("ClassName");
96                         MDC.remove(MDC_ALERT_SEVERITY);
97                 }
98         }
99
100         /**
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
104          * 
105          * @param classLogger
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
110          * @param th
111          *            Throwable; ignored if null
112          * @param param
113          *            Array of Strings used to build the message.
114          */
115         @SuppressWarnings("static-access")
116         public static void logEcompError(EELFLoggerDelegate classLogger, EPAppMessagesEnum epMessageEnum, Throwable th,
117                         String... param) {
118
119                 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
120                 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
121
122                 MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.name());
123                 MDC.put("ErrorCode", epMessageEnum.getErrorCode());
124                 MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
125
126                 final String message = EPLogUtil.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(),
127                                 (Object[]) param);
128                 if (errorSeverityEnum == ErrorSeverityEnum.INFO) {
129                         if (th == null)
130                                 classLogger.info(classLogger.errorLogger, message);
131                         else
132                                 classLogger.info(classLogger.errorLogger, message, th);
133                 } else if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
134                         if (th == null)
135                                 classLogger.warn(classLogger.errorLogger, message);
136                         else
137                                 classLogger.warn(classLogger.errorLogger, message, th);
138                 } else {
139                         if (th == null)
140                                 classLogger.error(classLogger.errorLogger, message);
141                         else
142                                 classLogger.error(classLogger.errorLogger, message, th);
143                 }
144
145                 // Clean up
146                 MDC.remove(MDC_ALERT_SEVERITY);
147                 MDC.remove("ErrorCode");
148                 MDC.remove("ErrorDescription");
149         }
150
151         /**
152          * Builds a string using the format and parameters.
153          * 
154          * @param message
155          * @param args
156          * @return
157          */
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));
163                 } else {
164                         sbFormattedMessage.append(message);
165                 }
166                 return sbFormattedMessage.toString();
167         }
168
169         /**
170          * Builds a comma-separated string of values to document a user action.
171          * 
172          * @param action
173          *            String
174          * @param activity
175          *            String
176          * @param userId
177          *            String
178          * @param affectedId
179          *            String
180          * @param comment
181          *            String
182          * @return Value suitable for writing to the audit log file.
183          */
184         public static String formatAuditLogMessage(String action, String activity, String userId, String affectedId,
185                         String comment) {
186                 StringBuilder auditLogMsg = new StringBuilder();
187                 auditLogMsg.append("Click_A:[");
188                 if (action != null && !action.equals("")) {
189                         auditLogMsg.append(" Action: ");
190                         auditLogMsg.append(action);
191                 }
192
193                 if (activity != null && !activity.equals("")) {
194                         auditLogMsg.append(",Activity CD: ");
195                         auditLogMsg.append(activity);
196                 }
197
198                 if (userId != null && !userId.equals("")) {
199                         auditLogMsg.append(",User ID: ");
200                         auditLogMsg.append(userId);
201                 }
202
203                 if (affectedId != null && !affectedId.equals("")) {
204                         auditLogMsg.append(",Affected ID: ");
205                         auditLogMsg.append(affectedId);
206                 }
207
208                 if (comment != null && !comment.equals("")) {
209                         auditLogMsg.append(",Comment: ");
210                         auditLogMsg.append(comment);
211                 }
212                 auditLogMsg.append("]");
213                 return auditLogMsg.toString();
214         }
215
216         /**
217          * Builds a comma-separated string of values to document a user browser
218          * action.
219          * 
220          * @param orgUserId
221          *            String
222          * @param appName
223          *            String
224          * @param action
225          *            String
226          * @param activity
227          *            String
228          * @param actionLink
229          *            String
230          * @param page
231          *            String
232          * @param function
233          *            String
234          * @param type
235          *            String
236          * @return String value suitable for writing to the audit log file.
237          */
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);
245                 }
246
247                 if (appName != null && !appName.equals("")) {
248                         auditLogStoreAnalyticsMsg.append(",AppName: ");
249                         auditLogStoreAnalyticsMsg.append(appName);
250                 }
251
252                 if (action != null && !action.equals("")) {
253                         auditLogStoreAnalyticsMsg.append(",Action: ");
254                         auditLogStoreAnalyticsMsg.append(action);
255                 }
256
257                 if (activity != null && !activity.equals("")) {
258                         auditLogStoreAnalyticsMsg.append(",Activity: ");
259                         auditLogStoreAnalyticsMsg.append(activity);
260                 }
261
262                 if (actionLink != null && !actionLink.equals("")) {
263                         auditLogStoreAnalyticsMsg.append(",ActionLink: ");
264                         auditLogStoreAnalyticsMsg.append(actionLink);
265                 }
266
267                 if (page != null && !page.equals("")) {
268                         auditLogStoreAnalyticsMsg.append(",Page: ");
269                         auditLogStoreAnalyticsMsg.append(page);
270                 }
271
272                 if (function != null && !function.equals("")) {
273                         auditLogStoreAnalyticsMsg.append(",Function: ");
274                         auditLogStoreAnalyticsMsg.append(function);
275                 }
276
277                 if (type != null && !type.equals("")) {
278                         auditLogStoreAnalyticsMsg.append(",Type: ");
279                         auditLogStoreAnalyticsMsg.append(type);
280                 }
281                 auditLogStoreAnalyticsMsg.append("]");
282                 return auditLogStoreAnalyticsMsg.toString();
283         }
284
285 }