Catalog alignment
[sdc.git] / common-app-logging / src / main / java / org / openecomp / sdc / common / log / wrappers / Logger.java
1 package org.openecomp.sdc.common.log.wrappers;
2
3 import com.google.common.annotations.VisibleForTesting;
4 import org.onap.logging.ref.slf4j.ONAPLogConstants;
5 import org.openecomp.sdc.common.log.elements.*;
6 import org.openecomp.sdc.common.log.enums.EcompErrorSeverity;
7 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
8 import org.openecomp.sdc.common.log.enums.LogLevel;
9 import org.slf4j.Marker;
10
11 import java.util.UUID;
12
13
14 /**
15  * This class wraps {@link org.slf4j.Logger} object and provides mandatory information required by Ecomp logging rules.
16  * Note: All deprecated methods are supported to be compatible to the legacy code
17  * and have not be used by the new code
18  */
19 public class Logger implements org.slf4j.Logger {
20     private final LoggerDebug debug;
21     private final LoggerError error;
22     private final LoggerMetric metric;
23     private final org.slf4j.Logger logger;
24
25     @VisibleForTesting
26     public Logger(org.slf4j.Logger logger) {
27         this.logger = logger;
28         this.debug = LoggerFactory.getMdcLogger(LoggerDebug.class, logger);
29         this.error = LoggerFactory.getMdcLogger(LoggerError.class, logger);
30         this.metric = LoggerFactory.getMdcLogger(LoggerMetric.class, logger);
31
32     }
33
34     private Logger(String className) {
35         this(org.slf4j.LoggerFactory.getLogger(className));
36     }
37
38     public static Logger getLogger(String className) {
39         return new Logger(className);
40     }
41
42     public static Logger getLogger(Class<?> clazz) {
43         return new Logger(clazz.getName());
44     }
45
46
47     public boolean isDebugEnabled() {
48         return logger.isDebugEnabled();
49     }
50
51     @Override
52     public String getName() {
53         return logger.getName();
54     }
55
56     public boolean isTraceEnabled() {
57         return logger.isTraceEnabled();
58     }
59
60     public boolean isErrorEnabled() { return logger.isErrorEnabled(); }
61
62     public boolean isWarnEnabled() { return logger.isWarnEnabled(); }
63
64     @Override
65     @Deprecated /** Please use method {@link #warn(EcompLoggerErrorCode, String, String)}  **/
66     public void warn(String msg) {
67         if (isWarnEnabled()) {
68             error.log(LogLevel.WARN, msg);
69         }
70     }
71
72     @Override
73     @Deprecated /** Please use method {@link #warn(EcompLoggerErrorCode, String, String)}   **/
74     public void warn(String msg, Object o) {
75
76         if (isWarnEnabled()) {
77             error.log(LogLevel.WARN, msg, o);
78         }
79     }
80
81     public boolean isInfoEnabled() { return logger.isInfoEnabled(); }
82
83     @Override
84     public void info(String msg) {
85         if (isInfoEnabled()) {
86             debug.log(LogLevel.INFO, msg);
87         }
88     }
89
90     @Override
91     public void info(String msg, Object o) {
92         if (isInfoEnabled()) {
93             debug.log(LogLevel.INFO, msg, o);
94         }
95     }
96
97     @Override
98     public void info(String msg, Object o, Object o1) {
99         if (isInfoEnabled()) {
100             debug.log(LogLevel.INFO, msg, o, o1);
101         }
102     }
103
104     public void debug(String msg, Object... params) {
105         if (isDebugEnabled()) {
106             debug.log(LogLevel.DEBUG, msg, params);
107         }
108     }
109
110     public void metric(String msg, Object... params) {
111             metric.log(LogLevel.INFO, msg, params);
112     }
113
114     public void invoke(String targetEntity, String targetServiceName, String serviceName, String msg, Object... params) {
115
116         String invocationId = UUID.randomUUID().toString();
117         String requestID = UUID.randomUUID().toString();
118
119         metric.startTimer()
120             .stopTimer()
121                 .setOutgoingInvocationId(invocationId)
122                 .setTargetServiceName(targetServiceName)
123                 .setTargetEntity(targetEntity)
124                 .setStatusCode(ONAPLogConstants.ResponseStatus.COMPLETE.name())
125                 .setKeyRequestId(requestID)
126                 .setServiceName(serviceName);
127         metric.log(ONAPLogConstants.Markers.INVOKE, LogLevel.INFO, msg, params);
128     }
129
130     public void invokeReturn(String msg, Object... params) {
131         try {
132             metric.startTimer()
133                     .stopTimer();
134         } catch (Exception e) {
135             e.printStackTrace();
136         }
137         metric.log(ONAPLogConstants.Markers.INVOKE_RETURN, LogLevel.INFO, msg, params);
138     }
139
140     public void invokeSynchronous(String msg, Object... params) {
141         metric.log(ONAPLogConstants.Markers.INVOKE_SYNCHRONOUS, LogLevel.INFO, msg, params);
142     }
143
144     @Override
145     public void debug(String msg, Throwable throwable) {
146         if (isDebugEnabled()) {
147             debug.log(LogLevel.DEBUG, msg, throwable);
148         }
149     }
150
151     @Override
152     public boolean isDebugEnabled(Marker marker) {
153         return false;
154     }
155
156     @Override
157     public void debug(Marker marker, String msg) {
158         if (isDebugEnabled()) {
159             debug.log(LogLevel.DEBUG, msg);
160         }
161     }
162
163     @Override
164     public void debug(Marker marker, String msg, Object o) {
165         if (isDebugEnabled()) {
166             debug.log(LogLevel.DEBUG, msg, o);
167         }
168     }
169
170     @Override
171     public void debug(Marker marker, String msg, Object o, Object o1) {
172         if (isDebugEnabled()) {
173             debug.log(LogLevel.DEBUG, msg, o, o1);
174         }
175     }
176
177     @Override
178     public void debug(Marker marker, String msg, Object... objects) {
179         if (isDebugEnabled()) {
180             debug.log(LogLevel.DEBUG, msg, objects);
181         }
182     }
183
184     @Override
185     public void debug(Marker marker, String msg, Throwable throwable) {
186         if (isDebugEnabled()) {
187             debug.log(LogLevel.DEBUG, msg, throwable);
188         }
189     }
190
191     public void debug(String message) {
192         if (isDebugEnabled()) {
193             debug.log(LogLevel.DEBUG, message);
194         }
195     }
196
197     @Override
198     public void debug(String msg, Object o) {
199         if (isDebugEnabled()) {
200             debug.log(LogLevel.DEBUG, msg, o);
201         }
202     }
203
204     @Override
205     public void debug(String msg, Object o, Object o1) {
206         if (isDebugEnabled()) {
207             debug.log(LogLevel.DEBUG, msg, o, o1);
208         }
209     }
210
211     public void trace(String message, Object... params) {
212         if (isTraceEnabled()) {
213             debug.log(LogLevel.TRACE, message, params);
214         }
215     }
216
217     @Override
218     public void trace(String msg, Throwable throwable) {
219         if (isTraceEnabled()) {
220             debug.log(LogLevel.TRACE, msg, throwable);
221         }
222     }
223
224     @Override
225     public boolean isTraceEnabled(Marker marker) {
226         return false;
227     }
228
229     @Override
230     public void trace(Marker marker, String msg) {
231         if (isTraceEnabled()) {
232             debug.log(LogLevel.TRACE, msg);
233         }
234     }
235
236     @Override
237     public void trace(Marker marker, String msg, Object o) {
238         if (isTraceEnabled()) {
239             debug.log(LogLevel.TRACE, msg, o);
240         }
241     }
242
243     @Override
244     public void trace(Marker marker, String msg, Object o, Object o1) {
245         if (isTraceEnabled()) {
246             debug.log(LogLevel.TRACE, msg, o, o1);
247         }
248     }
249
250     @Override
251     public void trace(Marker marker, String msg, Object... objects) {
252         if (isTraceEnabled()) {
253             debug.log(LogLevel.TRACE, msg, objects);
254         }
255     }
256
257     @Override
258     public void trace(Marker marker, String msg, Throwable throwable) {
259         if (isTraceEnabled()) {
260             debug.log(LogLevel.TRACE, msg, throwable);
261         }
262     }
263
264     public void trace(String msg) {
265         if (isTraceEnabled()) {
266             debug.log(LogLevel.TRACE, msg);
267         }
268     }
269
270     @Override
271     public void trace(String msg, Object o) {
272         if (isTraceEnabled()) {
273             debug.log(LogLevel.TRACE, msg, o);
274         }
275     }
276
277     @Override
278     public void trace(String msg, Object o, Object o1) {
279         if (isTraceEnabled()) {
280             debug.log(LogLevel.TRACE, msg, o, o1);
281         }
282     }
283
284     public void info(String msg, Object... params) {
285         if (isInfoEnabled()) {
286             debug.log(LogLevel.INFO, msg, params);
287         }
288     }
289
290     @Override
291     public void info(String msg, Throwable throwable) {
292         if (isInfoEnabled()) {
293             debug.log(LogLevel.INFO, msg, throwable);
294         }
295     }
296
297     @Override
298     public boolean isInfoEnabled(Marker marker) {
299         return false;
300     }
301
302     @Override
303     public void info(Marker marker, String msg) {
304         if (isInfoEnabled()) {
305             debug.log(LogLevel.INFO, msg);
306         }
307     }
308
309     @Override
310     public void info(Marker marker, String msg, Object o) {
311         if (isInfoEnabled()) {
312             debug.log(LogLevel.INFO, msg, o);
313         }
314     }
315
316     @Override
317     public void info(Marker marker, String msg, Object o, Object o1) {
318         if (isInfoEnabled()) {
319             debug.log(LogLevel.INFO, msg, o, o1);
320         }
321     }
322
323     @Override
324     public void info(Marker marker, String msg, Object... objects) {
325         if (isInfoEnabled()) {
326             debug.log(LogLevel.INFO, msg, objects);
327         }
328     }
329
330     @Override
331     public void info(Marker marker, String msg, Throwable throwable) {
332         if (isInfoEnabled()) {
333             debug.log(LogLevel.INFO, msg, throwable);
334         }
335     }
336
337     @Deprecated /** Please use method {@link #warn(EcompLoggerErrorCode, String, String)}   **/
338     public void warn(String msg, Object... params){
339         if (isWarnEnabled()) {
340             error.log(LogLevel.WARN, msg, params);
341         }
342     }
343
344     @Override
345     @Deprecated /** Please use method {@link #warn(EcompLoggerErrorCode, String, String)}   **/
346     public void warn(String msg, Object o, Object o1) {
347         if (isWarnEnabled()) {
348             error.log(LogLevel.WARN, msg, o, o1);
349         }
350     }
351
352     @Override
353     @Deprecated /** Please use method {@link #warn(EcompLoggerErrorCode, String, String)}   **/
354     public void warn(String msg, Throwable throwable) {
355         if (isWarnEnabled()) {
356             error.log(LogLevel.WARN, msg, throwable);
357         }
358     }
359
360     @Override
361     public boolean isWarnEnabled(Marker marker) {
362         return false;
363     }
364
365     @Override
366     @Deprecated /** Please use method {@link #warn(EcompLoggerErrorCode, String, String)}   **/
367     public void warn(Marker marker, String msg) {
368         if (isWarnEnabled()) {
369             error.log(LogLevel.WARN, msg);
370         }
371     }
372
373     @Override
374     @Deprecated /** Please use method {@link #warn(EcompLoggerErrorCode, String, String)}   **/
375     public void warn(Marker marker, String msg, Object o) {
376         if (isWarnEnabled()) {
377             error.log(LogLevel.WARN, msg, o);
378         }
379     }
380
381     @Override
382     @Deprecated /** Please use method {@link #warn(EcompLoggerErrorCode, String, String)}   **/
383     public void warn(Marker marker, String msg, Object o, Object o1) {
384         if (isWarnEnabled()) {
385             error.log(LogLevel.WARN, msg, o, o1);
386         }
387     }
388
389     @Override
390     @Deprecated /** Please use method {@link #warn(EcompLoggerErrorCode, String, String)}   **/
391     public void warn(Marker marker, String msg, Object... objects) {
392         if (isWarnEnabled()) {
393             error.log(LogLevel.WARN, msg, objects);
394         }
395     }
396
397     @Override
398     @Deprecated /** Please use method {@link #warn(EcompLoggerErrorCode, String, String)}   **/
399     public void warn(Marker marker, String msg, Throwable throwable) {
400         if (isWarnEnabled()) {
401             error.log(LogLevel.WARN, msg, throwable);
402         }
403     }
404
405     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, String)}  **/
406     public void error(String msg, Object... params){
407         if (isErrorEnabled()) {
408             error.log(LogLevel.ERROR, msg, params);
409         }
410     }
411
412     @Override
413     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, String)}  **/
414     public void error(String msg, Throwable throwable) {
415         if (isErrorEnabled()) {
416             error.log(LogLevel.ERROR, msg, throwable);
417         }
418     }
419
420     @Override
421     public boolean isErrorEnabled(Marker marker) {
422         return false;
423     }
424
425     @Override
426     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, String)}  **/
427     public void error(Marker marker, String msg) {
428         if (isErrorEnabled()) {
429             error.log(LogLevel.ERROR, msg);
430         }
431     }
432
433     @Override
434     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, String)}  **/
435     public void error(Marker marker, String msg, Object o) {
436         if (isErrorEnabled()) {
437             error.log(LogLevel.ERROR, msg, o);
438         }
439     }
440
441     @Override
442     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, String)}  **/
443     public void error(Marker marker, String msg, Object o, Object o1) {
444         if (isErrorEnabled()) {
445             error.log(LogLevel.ERROR, msg, o, o1);
446         }
447     }
448
449     @Override
450     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, String)}  **/
451     public void error(Marker marker, String msg, Object... params) {
452         if (isErrorEnabled()) {
453             error.log(LogLevel.ERROR, msg, params);
454         }
455     }
456
457     @Override
458     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, String)}  **/
459     public void error(Marker marker, String msg, Throwable throwable) {
460         if (isErrorEnabled()) {
461             error.log(LogLevel.ERROR, msg, throwable);
462         }
463     }
464
465     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, String)}  **/
466     public void error(String msg){
467         if (isErrorEnabled()) {
468             error.log(LogLevel.ERROR, msg);
469         }
470     }
471
472     @Override
473     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, String)}  **/
474     public void error(String msg, Object o) {
475         if (isErrorEnabled()) {
476             error.log(LogLevel.ERROR, msg, o);
477         }
478     }
479
480     @Override
481     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, String)}  **/
482     public void error(String msg, Object o, Object o1) {
483         if (isErrorEnabled()) {
484             error.log(LogLevel.ERROR, msg, o, o1);
485         }
486     }
487
488     /**
489      * Writes out ERROR logging level message to the application error log
490      * @param errorLevel code representing the error severity level
491      * @param serviceName name of the API invoked at the logging component
492      * @param targetEntity name of the ECOMP component or sub-component, or external entity at which the error occurred or null
493      * @param errorDescription a human readable description of the error condition
494      * @param params optional parameters of a given error description
495      */
496     public void error(EcompErrorSeverity errorLevel,
497                       EcompLoggerErrorCode errorCodeEnum,
498                       String serviceName,
499                       String targetEntity,
500                       String errorDescription, Object...params) {
501         if (isErrorEnabled()) {
502             error.log(errorLevel, errorCodeEnum, serviceName, targetEntity, errorDescription, params);
503         }
504     }
505
506     /**
507      * Writes out ERROR logging level message to the application error log
508      * @param errorCodeEnum code representing the error condition
509      * @param serviceName name of the API invoked at the logging component
510      * @param targetEntity name of the ECOMP component or sub-component, or external entity at which the error occurred or null
511      * @param errorDescription a human readable description of the error condition
512      * @param params optional parameters of a given error description
513      */
514     @Deprecated /** Please use method {@link #error(EcompLoggerErrorCode, String, ErrorLogOptionalData, String, Object...)}   **/
515     public void error(EcompLoggerErrorCode errorCodeEnum,
516                       String serviceName,
517                       String targetEntity,
518                       String errorDescription, Object...params) {
519         if (isErrorEnabled()) {
520             error.log(LogLevel.ERROR, errorCodeEnum, serviceName, targetEntity, errorDescription, params);
521         }
522     }
523
524     /* Writes out ERROR logging level message to the application error LOG
525      * @param errorCodeEnum code representing the error condition
526      * @param errorDescription a human readable description of the error condition
527      * @param params optional parameters of a given error description
528      */
529
530     public void error(EcompLoggerErrorCode errorCodeEnum, String serviceName,
531                       String errorDescription, Object...params) {
532         error(errorCodeEnum, serviceName, (String)null, errorDescription, params);
533     }
534
535     /**
536      * Writes out ERROR logging level message to the application error log
537      * @param errorCodeEnum code representing the error condition
538      * @param serviceName name of the API invoked at the logging component
539      * @param errorLogOptionalData elements that contans all relevant data of the error
540      * @param errorDescription a human readable description of the error condition
541      * @param params optional parameters of a given error description
542      */
543     public void error(EcompLoggerErrorCode errorCodeEnum,
544                       String serviceName,
545                       ErrorLogOptionalData errorLogOptionalData,
546                       String errorDescription,
547                       Object...params) {
548         if (isErrorEnabled()) {
549             error.log(LogLevel.ERROR, errorCodeEnum, serviceName, errorLogOptionalData, errorDescription, params);
550         }
551     }
552
553     /**
554      * Writes out WARN logging level message to the application error log
555      * @param errorCodeEnum code representing the error condition
556      * @param serviceName name of the API invoked at the logging component
557      * @param targetEntity name of the ECOMP component or sub-component, or external entity at which the error occurred or null
558      * @param errorDescription a human readable description of the error condition
559      * @param params optional parameters of a given error description
560      */
561     public void warn(EcompLoggerErrorCode errorCodeEnum,
562                       String serviceName,
563                       String targetEntity,
564                       String errorDescription, Object...params) {
565         if (isWarnEnabled()) {
566             error.log(LogLevel.WARN, errorCodeEnum, serviceName, targetEntity, errorDescription, params);
567         }
568     }
569
570     /**
571      * Writes out WARN logging level message to the application error log
572      * @param errorCodeEnum code representing the error condition
573      * @param serviceName name of the API invoked at the logging component
574      * @param errorLogOptionalData elements that contans all relevant data of the error
575      * @param description a human readable description of the error condition
576      * @param params optional parameters of a given error description
577      */
578     public void warn(EcompLoggerErrorCode errorCodeEnum,
579                      String serviceName,
580                      ErrorLogOptionalData errorLogOptionalData,
581                      String description, Object...params) {
582         if (isWarnEnabled()) {
583             error.log(LogLevel.WARN, errorCodeEnum, serviceName, errorLogOptionalData, description, params);
584         }
585     }
586
587     /**
588      * Writes out WARN logging level message to the application error log
589      * @param errorCodeEnum code representing the error condition
590      * @param serviceName name of the API invoked at the logging component
591      * @param errorDescription a human readable description of the error condition
592      * @param params optional parameters of a given error description
593      */
594     public void warn(EcompLoggerErrorCode errorCodeEnum,
595                      String serviceName,
596                      String errorDescription, Object...params) {
597         if (isWarnEnabled()) {
598             error.log(LogLevel.WARN, errorCodeEnum, serviceName, (String)null, errorDescription, params);
599         }
600     }
601
602     /**
603      * Writes out FATAL logging level message to the application error log
604      * @param errorCodeEnum code representing the error condition
605      * @param serviceName name of the API invoked at the logging component
606      * @param targetEntity name of the ECOMP component or sub-component, or external entity at which the error occurred or null
607      * @param errorDescription a human readable description of the error condition
608      * @param params optional parameters of a given error description
609      */
610     public void fatal(EcompLoggerErrorCode errorCodeEnum,
611                       String serviceName,
612                       String targetEntity,
613                       String errorDescription, Object...params) {
614         if (isErrorEnabled()) {
615             error.log(LogLevel.FATAL, errorCodeEnum, serviceName, targetEntity, errorDescription, params);
616         }
617     }
618
619     /**
620      * Writes out FATAL logging level message to the application error log
621      * @param errorCodeEnum code representing the error condition
622      * @param serviceName name of the API invoked at the logging component
623      * @param errorLogOptionalData elements that contans all relevant data of the error
624      * @param description a human readable description of the error condition
625      * @param params optional parameters of a given error description
626      */
627     public void fatal(EcompLoggerErrorCode errorCodeEnum, String serviceName,
628                       ErrorLogOptionalData errorLogOptionalData,
629                       String description, Object...params) {
630         if (isErrorEnabled()) {
631             error.log(LogLevel.FATAL, errorCodeEnum, serviceName, errorLogOptionalData, description, params);
632         }
633     }
634 }
635