2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.logging.slf4j;
19 import org.openecomp.sdc.logging.api.AuditData;
20 import org.openecomp.sdc.logging.api.Logger;
21 import org.slf4j.LoggerFactory;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
27 import static org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.PREFIX;
33 class SLF4JLoggerWrapper implements Logger {
35 private static final String BEGIN_TIMESTAMP = PREFIX + "BeginTimestamp";
36 private static final String END_TIMESTAMP = PREFIX + "EndTimestamp";
37 private static final String ELAPSED_TIME = PREFIX + "ElapsedTime";
38 private static final String STATUS_CODE = PREFIX + "StatusCode";
39 private static final String RESPONSE_CODE = PREFIX + "ResponseCode";
40 private static final String RESPONSE_DESCRIPTION = PREFIX + "ResponsDescription";
41 private static final String CLIENT_IP_ADDRESS = PREFIX + "ClientIpAddress";
43 //The specified format presents time in UTC formatted per ISO 8601, as required by ONAP logging guidelines
44 private final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
45 private final org.slf4j.Logger logger;
47 SLF4JLoggerWrapper(Class<?> clazz) {
48 logger = LoggerFactory.getLogger(clazz);
51 SLF4JLoggerWrapper(String className) {
52 logger = LoggerFactory.getLogger(className);
56 public String getName() {
57 return logger.getName();
61 public boolean isMetricsEnabled() {
62 return logger.isInfoEnabled(Markers.METRICS);
66 public void metrics(String msg) {
67 logger.info(Markers.METRICS, msg);
71 public void metrics(String msg, Object arg) {
72 logger.info(Markers.METRICS, msg, arg);
76 public void metrics(String msg, Object arg1, Object arg2) {
77 logger.info(Markers.METRICS, msg, arg1, arg2);
81 public void metrics(String msg, Object... arguments) {
82 logger.info(Markers.METRICS, msg, arguments);
86 public void metrics(String msg, Throwable t) {
87 logger.info(Markers.METRICS, msg, t);
91 public boolean isAuditEnabled() {
92 return logger.isInfoEnabled(Markers.AUDIT);
96 public void audit(AuditData data) {
102 MDC.put(BEGIN_TIMESTAMP, DATE_FORMAT.format(new Date(data.getStartTime())));
103 MDC.put(END_TIMESTAMP, DATE_FORMAT.format(new Date(data.getEndTime())));
104 MDC.put(ELAPSED_TIME, String.valueOf(data.getEndTime() - data.getStartTime()));
106 if (data.getStatusCode() != null) {
107 MDC.put(STATUS_CODE, data.getStatusCode() == AuditData.StatusCode.COMPLETE ? "COMPLETE" : "ERROR");
110 if (data.getResponseCode() != null) {
111 MDC.put(RESPONSE_CODE, data.getResponseCode());
114 if (data.getResponseDescription() != null) {
115 MDC.put(RESPONSE_DESCRIPTION, data.getResponseDescription());
118 if (data.getClientIpAddress() != null) {
119 MDC.put(CLIENT_IP_ADDRESS, data.getClientIpAddress());
123 logger.info(Markers.AUDIT, "");
125 MDC.remove(BEGIN_TIMESTAMP);
126 MDC.remove(END_TIMESTAMP);
127 MDC.remove(ELAPSED_TIME);
128 MDC.remove(STATUS_CODE);
129 MDC.remove(RESPONSE_CODE);
130 MDC.remove(RESPONSE_DESCRIPTION);
131 MDC.remove(CLIENT_IP_ADDRESS);
136 public boolean isDebugEnabled() {
137 return logger.isDebugEnabled();
141 public void debug(String msg) {
146 public void debug(String format, Object arg) {
147 logger.debug(format, arg);
151 public void debug(String format, Object arg1, Object arg2) {
152 logger.debug(format, arg1, arg2);
156 public void debug(String format, Object... arguments) {
157 logger.debug(format, arguments);
161 public void debug(String msg, Throwable t) {
162 logger.debug(msg, t);
166 public boolean isInfoEnabled() {
167 return logger.isInfoEnabled();
171 public void info(String msg) {
176 public void info(String format, Object arg) {
177 logger.info(format, arg);
181 public void info(String format, Object arg1, Object arg2) {
182 logger.info(format, arg1, arg2);
186 public void info(String format, Object... arguments) {
187 logger.info(format, arguments);
191 public void info(String msg, Throwable t) {
196 public boolean isWarnEnabled() {
197 return logger.isWarnEnabled();
201 public void warn(String msg) {
206 public void warn(String format, Object arg) {
207 logger.warn(format, arg);
211 public void warn(String format, Object... arguments) {
212 logger.warn(format, arguments);
216 public void warn(String format, Object arg1, Object arg2) {
217 logger.warn(format, arg1, arg2);
221 public void warn(String msg, Throwable t) {
226 public boolean isErrorEnabled() {
227 return logger.isErrorEnabled();
231 public void error(String msg) {
236 public void error(String format, Object arg) {
237 logger.error(format, arg);
241 public void error(String format, Object arg1, Object arg2) {
242 logger.error(format, arg1, arg2);
246 public void error(String format, Object... arguments) {
247 logger.error(format, arguments);
251 public void error(String msg, Throwable t) {
252 logger.error(msg, t);