2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.log.elements;
23 import org.apache.commons.lang3.StringUtils;
24 import org.openecomp.sdc.common.config.EcompErrorConfiguration;
25 import org.openecomp.sdc.common.log.api.ILogConfiguration;
26 import org.openecomp.sdc.common.log.api.ILogFieldsHandler;
27 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
28 import org.openecomp.sdc.common.log.enums.LogLevel;
29 import org.openecomp.sdc.common.log.enums.LogMarkers;
30 import org.slf4j.Logger;
31 import org.slf4j.MarkerFactory;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
38 public class LoggerError extends LoggerBase {
39 private static ArrayList<String> mandatoryFields = new ArrayList<>(Arrays.asList(
40 ILogConfiguration.MDC_KEY_REQUEST_ID,
41 ILogConfiguration.MDC_SERVICE_NAME,
42 ILogConfiguration.MDC_ERROR_CATEGORY,
43 ILogConfiguration.MDC_ERROR_CODE));
45 public static final String defaultServiceName = "SDC catalog";
47 LoggerError(ILogFieldsHandler ecompMdcWrapper, Logger logger) {
48 super(ecompMdcWrapper, MarkerFactory.getMarker(LogMarkers.ERROR_MARKER.text()), logger);
52 public List<String> getMandatoryFields() {
53 return Collections.unmodifiableList(mandatoryFields);
57 public LoggerError setKeyRequestId(String keyRequestId) {
58 return (LoggerError) super.setKeyRequestId(keyRequestId);
62 public LoggerError startTimer() {
67 public LoggerError clear() {
68 ecompLogFieldsHandler.removeErrorCategory();
69 ecompLogFieldsHandler.removeErrorDescription();
70 ecompLogFieldsHandler.removeErrorCode();
74 public void log(LogLevel logLevel,
75 EcompLoggerErrorCode errorCodeEnum,
78 String message, Object... params) {
79 fillFieldsBeforeLogging(logLevel, errorCodeEnum, serviceName, targetEntity);
80 super.log(logLevel, message, params);
83 private void fillFieldsBeforeLogging(LogLevel logLevel, EcompLoggerErrorCode errorCodeEnum, String serviceName, String targetEntity) {
85 ecompLogFieldsHandler.setErrorCode(errorCodeEnum.getErrorCode());
86 ecompLogFieldsHandler.setErrorCategory(logLevel.name());
87 if (!StringUtils.isEmpty(targetEntity)) {
88 //avoid overriding this parameter in MDC
89 ecompLogFieldsHandler.setTargetEntity(targetEntity);
91 if (StringUtils.isEmpty(ecompLogFieldsHandler.getServiceName())) {
92 ecompLogFieldsHandler.setServiceName(serviceName);
94 setKeyRequestIdIfNotSetYet();
97 public void log(EcompErrorConfiguration.EcompErrorSeverity errorSeverity,
98 EcompLoggerErrorCode errorCodeEnum,
101 String message, Object... params) {
102 log(convertFromSeverityErrorLevel(errorSeverity), errorCodeEnum, serviceName, targetEntity, message, params);
105 public void log(LogLevel logLevel,
106 EcompLoggerErrorCode errorCodeEnum,
108 String message, Object... params) {
109 log(logLevel, errorCodeEnum, serviceName, null, message, params);
112 public void log(LogLevel logLevel,
113 EcompLoggerErrorCode errorCodeEnum,
116 log(logLevel, errorCodeEnum, serviceName, message);
120 public void log(LogLevel logLevel, String message, Object... params) {
121 log(logLevel, EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, defaultServiceName, null, message, params);
124 public void log(LogLevel logLevel, String message, Throwable throwable) {
125 log(logLevel, createErrorMessage(message, throwable));
128 public void log(LogLevel logLevel, String message) {
129 log(logLevel, EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, defaultServiceName, null, message);
132 public void logInfo(LogLevel logLevel, String message, Object... params) {
133 log(logLevel, EcompLoggerErrorCode.SUCCESS, defaultServiceName, null, message, params);
136 private LogLevel convertFromSeverityErrorLevel(EcompErrorConfiguration.EcompErrorSeverity severityLevel) {
137 switch (severityLevel) {
139 return LogLevel.INFO;
141 return LogLevel.FATAL;
143 return LogLevel.ERROR;
145 return LogLevel.WARN;
147 return LogLevel.ERROR;