2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.common.config;
23 import fj.data.Either;
24 import org.apache.commons.lang3.StringUtils;
25 import org.openecomp.sdc.common.log.enums.EcompErrorSeverity;
26 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import java.util.Formatter;
31 import java.util.IllegalFormatException;
32 import java.util.Locale;
34 public class EcompErrorLogUtil {
36 private static String ECOMP_ERROR_TMPL = "ETYPE = \"%s\" ENAME = \"%s\" ECODE = \"%s\" ECONTEXT = \"%s\" EDESC = \"%s\"";
37 private static Logger log = Logger.getLogger(EcompErrorLogUtil.class.getName());
38 private static final String FATAL_ERROR_PREFIX = "FATAL ERROR!! ";
40 public static void logEcompError(EcompErrorName ecompErrorName, EcompErrorInfo ecompErrorInfo,
41 String ecompErrorContext, String... ecompDescriptionParams) {
42 if (ecompErrorInfo != null) {
43 StringBuilder sb = new StringBuilder();
44 Formatter formatter = new Formatter(sb, Locale.US);
46 String description = ecompErrorInfo.getDescription();
47 String severityStr = ecompErrorInfo.getSeverity();
48 EcompErrorSeverity severity = EcompErrorSeverity.ERROR;
49 // Since there is no FATAL log level, this is how we distinguish
51 if (severityStr.equals(EcompErrorSeverity.FATAL.name())) {
52 description = FATAL_ERROR_PREFIX + description;
53 severity = EcompErrorSeverity.FATAL;
54 } else if (severityStr.equals(EcompErrorSeverity.WARN.name())) {
55 severity = EcompErrorSeverity.WARN;
56 } else if (severityStr.equals(EcompErrorSeverity.INFO.name())) {
57 severity = EcompErrorSeverity.INFO;
60 MDC.put("alarmSeverity", ecompErrorInfo.getAlarmSeverity());
61 // else it stays ERROR
62 formatter.format(ECOMP_ERROR_TMPL, ecompErrorInfo.getType(), ecompErrorName.name(),
63 ecompErrorInfo.getCode(), ecompErrorContext, description);
64 log.error(severity, EcompLoggerErrorCode.getByValue(ecompErrorInfo.getCode()),
65 ecompErrorContext, ecompErrorContext, description);
68 MDC.remove("alarmSeverity");
73 public static void logEcompError(String ecompErrorContext, EcompErrorEnum ecompErrorEnum,
74 String... ecompDescriptionParams) {
75 logEcompError(ecompErrorContext, ecompErrorEnum, true, ecompDescriptionParams);
78 public static void logEcompError(String ecompErrorContext, EcompErrorEnum ecompErrorEnum, boolean logMissingParams,
79 String... ecompDescriptionParams) {
80 StringBuilder sb = new StringBuilder();
81 Formatter formatter = new Formatter(sb, Locale.US);
85 Either<String, Boolean> setDescriptionParamsResult = setDescriptionParams(ecompErrorEnum,
86 ecompDescriptionParams);
87 if (setDescriptionParamsResult.isLeft()) {
88 description = setDescriptionParamsResult.left().value();
90 EcompErrorEnum mismatchErrorEnum = EcompErrorEnum.EcompMismatchParam;
91 if (logMissingParams) {
92 logEcompError("logEcompError", mismatchErrorEnum, false, ecompErrorEnum.name());
94 log.info("Failed to log the error code {}", mismatchErrorEnum);
98 EcompClassification classification = ecompErrorEnum.getClassification();
100 EcompErrorSeverity severity = EcompErrorSeverity.ERROR;
101 // Since there is no FATAL log level, this is how we distinguish the
103 if (classification == EcompClassification.FATAL) {
104 description = FATAL_ERROR_PREFIX + description;
105 severity = EcompErrorSeverity.FATAL;
106 } else if (classification == EcompClassification.WARNING) {
107 severity = EcompErrorSeverity.WARN;
108 } else if (classification == EcompClassification.INFORMATION) {
109 severity = EcompErrorSeverity.INFO;
112 String eCode = createEcode(ecompErrorEnum);
114 MDC.put("alarmSeverity", ecompErrorEnum.alarmSeverity.name());
115 // else it stays ERROR
116 formatter.format(ECOMP_ERROR_TMPL, ecompErrorEnum.geteType(), ecompErrorEnum.name(), eCode,
117 ecompErrorContext, description);
119 log.error(severity, EcompLoggerErrorCode.getByValue(ecompErrorEnum.getEcompErrorCode().name()),
120 ecompErrorContext, ecompErrorContext, description);
123 MDC.remove("alarmSeverity");
127 public static String createEcode(EcompErrorEnum ecompErrorEnum) {
129 EcompClassification classification = ecompErrorEnum.getClassification();
130 String ecompErrorCode = ecompErrorEnum.getEcompErrorCode().name();
132 String ecodeNumber = ecompErrorCode.substring(ecompErrorCode.indexOf("_") + 1);
134 return "ASDC" + ecodeNumber + classification.getClassification();
137 private static Either<String, Boolean> setDescriptionParams(EcompErrorEnum ecompErrorEnum,
138 String... descriptionParams) {
139 String description = ecompErrorEnum.getEcompErrorCode().getDescription();
141 // Counting number of params in description
142 int countMatches = StringUtils.countMatches(description, AbsEcompErrorManager.PARAM_STR);
143 // Catching cases when there are more params passed than there are in
144 // the description (formatter will ignore extra params and won't throw
146 if (countMatches != descriptionParams.length) {
147 return Either.right(false);
149 // Setting params of the description if any
150 StringBuilder sb = new StringBuilder();
151 Formatter formatter = new Formatter(sb, Locale.US);
153 formatter.format(description, (Object[]) descriptionParams).toString();
154 return Either.left(formatter.toString());
155 } catch (IllegalFormatException e) {
156 // Number of passed params doesn't match number of params in config
158 return Either.right(false);