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.web.support.UserUtils;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
35 public class EPLogUtil {
36 private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
37 public static void logEcompError(EPAppMessagesEnum epMessageEnum, String... param) {
39 AlarmSeverityEnum alarmSeverityEnum = epMessageEnum.getAlarmSeverity();
40 ErrorSeverityEnum errorSeverityEnum = epMessageEnum.getErrorSeverity();
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());
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);
53 errorLogger.error(resolution);
55 } catch(Exception e) {
56 errorLogger.error("Failed to log the error code. Details: " + UserUtils.getStackTrace(e));
58 MDC.remove("ErrorCode");
59 MDC.remove("ErrorDescription");
60 MDC.remove("ClassName");
61 MDC.remove(MDC_ALERT_SEVERITY);
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));
71 sbFormattedMessage.append(message);
74 return sbFormattedMessage.toString();