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 java.text.Format;
20 import java.text.SimpleDateFormat;
21 import org.openecomp.sdc.logging.api.AuditData;
22 import org.openecomp.sdc.logging.api.Logger;
23 import org.slf4j.LoggerFactory;
30 class SLF4JLoggerWrapper implements Logger {
32 //The specified format presents time in UTC formatted per ISO 8601, as required by ONAP logging guidelines
34 private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
35 private static final String PREFIX = "";
37 static final String BEGIN_TIMESTAMP = PREFIX + "BeginTimestamp";
38 static final String END_TIMESTAMP = PREFIX + "EndTimestamp";
39 static final String ELAPSED_TIME = PREFIX + "ElapsedTime";
40 static final String STATUS_CODE = PREFIX + "StatusCode";
41 static final String RESPONSE_CODE = PREFIX + "ResponseCode";
42 static final String RESPONSE_DESCRIPTION = PREFIX + "ResponseDescription";
43 static final String CLIENT_IP_ADDRESS = PREFIX + "ClientIpAddress";
45 private static final String[] ALL_MDC_FIELDS = {
46 BEGIN_TIMESTAMP, END_TIMESTAMP, ELAPSED_TIME, STATUS_CODE,
47 RESPONSE_CODE, RESPONSE_DESCRIPTION, CLIENT_IP_ADDRESS
50 private final org.slf4j.Logger logger;
52 SLF4JLoggerWrapper(org.slf4j.Logger delegate) {
53 this.logger = delegate;
56 // May cause http://www.slf4j.org/codes.html#loggerNameMismatch
57 SLF4JLoggerWrapper(Class<?> clazz) {
58 this(LoggerFactory.getLogger(clazz));
61 SLF4JLoggerWrapper(String className) {
62 this(LoggerFactory.getLogger(className));
66 public String getName() {
67 return logger.getName();
71 public boolean isMetricsEnabled() {
72 return logger.isInfoEnabled(Markers.METRICS);
76 public void metrics(String msg) {
77 logger.info(Markers.METRICS, msg);
81 public void metrics(String msg, Object arg) {
82 logger.info(Markers.METRICS, msg, arg);
86 public void metrics(String msg, Object arg1, Object arg2) {
87 logger.info(Markers.METRICS, msg, arg1, arg2);
91 public void metrics(String msg, Object... arguments) {
92 logger.info(Markers.METRICS, msg, arguments);
96 public void metrics(String msg, Throwable t) {
97 logger.info(Markers.METRICS, msg, t);
101 public boolean isAuditEnabled() {
102 return logger.isInfoEnabled(Markers.AUDIT);
106 public void audit(AuditData data) {
109 return; // not failing if null
113 putIfNotNull(RESPONSE_CODE, data.getResponseCode());
114 putIfNotNull(RESPONSE_DESCRIPTION, data.getResponseDescription());
115 putIfNotNull(CLIENT_IP_ADDRESS, data.getClientIpAddress());
117 if (data.getStatusCode() != null) {
118 MDC.put(STATUS_CODE, data.getStatusCode().name());
122 logger.info(Markers.AUDIT, "");
124 for (String k : ALL_MDC_FIELDS) {
130 private void putIfNotNull(String key, String value) {
136 private void putTimes(AuditData data) {
137 // SimpleDateFormat is not thread-safe and cannot be a constant
138 Format dateTimeFormat = new SimpleDateFormat(DATE_TIME_PATTERN);
139 MDC.put(BEGIN_TIMESTAMP, dateTimeFormat.format(data.getStartTime()));
140 MDC.put(END_TIMESTAMP, dateTimeFormat.format(data.getEndTime()));
141 MDC.put(ELAPSED_TIME, String.valueOf(data.getEndTime() - data.getStartTime()));
145 public boolean isDebugEnabled() {
146 return logger.isDebugEnabled();
150 public void debug(String msg) {
155 public void debug(String format, Object arg) {
156 logger.debug(format, arg);
160 public void debug(String format, Object arg1, Object arg2) {
161 logger.debug(format, arg1, arg2);
165 public void debug(String format, Object... arguments) {
166 logger.debug(format, arguments);
170 public void debug(String msg, Throwable t) {
171 logger.debug(msg, t);
175 public boolean isInfoEnabled() {
176 return logger.isInfoEnabled();
180 public void info(String msg) {
185 public void info(String format, Object arg) {
186 logger.info(format, arg);
190 public void info(String format, Object arg1, Object arg2) {
191 logger.info(format, arg1, arg2);
195 public void info(String format, Object... arguments) {
196 logger.info(format, arguments);
200 public void info(String msg, Throwable t) {
205 public boolean isWarnEnabled() {
206 return logger.isWarnEnabled();
210 public void warn(String msg) {
215 public void warn(String format, Object arg) {
216 logger.warn(format, arg);
220 public void warn(String format, Object... arguments) {
221 logger.warn(format, arguments);
225 public void warn(String format, Object arg1, Object arg2) {
226 logger.warn(format, arg1, arg2);
230 public void warn(String msg, Throwable t) {
235 public boolean isErrorEnabled() {
236 return logger.isErrorEnabled();
240 public void error(String msg) {
245 public void error(String format, Object arg) {
246 logger.error(format, arg);
250 public void error(String format, Object arg1, Object arg2) {
251 logger.error(format, arg1, arg2);
255 public void error(String format, Object... arguments) {
256 logger.error(format, arguments);
260 public void error(String msg, Throwable t) {
261 logger.error(msg, t);