nexus site path corrected
[portal.git] / ecomp-portal-BE / 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.web.support.UserUtils;
30 import org.slf4j.MDC;
31
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34
35 public class EPLogUtil {
36         private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
37         public static void logEcompError(EPAppMessagesEnum epMessageEnum, String... param) {
38                 try {
39                         AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
40                         ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
41                         
42                         MDC.put(MDC_ALERT_SEVERITY, alarmSeverityEnum.name());
43                         MDC.put("ErrorCode", epMessageEnum.getErrorCode());
44                         MDC.put("ErrorDescription", epMessageEnum.getErrorDescription());
45                         MDC.put("ClassName", EPLogUtil.class.getName());
46                         
47                         String resolution = EPLogUtil.formatMessage(epMessageEnum.getDetails() + " " + epMessageEnum.getResolution(), (Object[]) param);
48                         if (errorSeverityEnum == ErrorSeverityEnum.WARN) {
49                                 errorLogger.warn(resolution);
50                         } else if(errorSeverityEnum == ErrorSeverityEnum.INFO) {
51                                 errorLogger.info(resolution);
52                         } else {
53                                 errorLogger.error(resolution);
54                         }
55                 } catch(Exception e) {
56                         errorLogger.error("Failed to log the error code. Details: " + UserUtils.getStackTrace(e));
57                 } finally {
58                         MDC.remove("ErrorCode");
59                         MDC.remove("ErrorDescription");
60                         MDC.remove("ClassName");
61                         MDC.remove(MDC_ALERT_SEVERITY);
62                 }
63         }
64         
65         public static String formatMessage(String message, Object...args) {
66                 StringBuilder sbFormattedMessage = new StringBuilder();
67                 if (args!=null && args.length>0 && message!=null && message != "") {
68                         MessageFormat mf = new MessageFormat(message);
69                         sbFormattedMessage.append(mf.format(args));
70                 } else {
71                         sbFormattedMessage.append(message);
72                 }
73                 
74                 return sbFormattedMessage.toString();
75         }
76 }