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