Various improvements
[dcaegen2/collectors/hv-ves.git] / hv-collector-utils / src / main / kotlin / org / onap / dcae / collectors / veshv / utils / logging / Logger.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.utils.logging
21
22 import org.slf4j.LoggerFactory
23 import kotlin.reflect.KClass
24
25 class Logger(val logger: org.slf4j.Logger) {
26     constructor(clazz: KClass<out Any>) : this(LoggerFactory.getLogger(clazz.java))
27     constructor(name: String) : this(LoggerFactory.getLogger(name))
28
29     //
30     // TRACE
31     //
32
33     val traceEnabled: Boolean
34         get() = logger.isTraceEnabled
35
36     fun trace(messageProvider: () -> String) {
37         if (logger.isTraceEnabled) {
38             logger.trace(messageProvider())
39         }
40     }
41
42
43     //
44     // DEBUG
45     //
46
47     fun debug(message: String) {
48         logger.debug(message)
49     }
50
51     fun debug(message: String, t: Throwable) {
52         logger.debug(message, t)
53     }
54
55     fun debug(messageProvider: () -> String) {
56         if (logger.isDebugEnabled) {
57             logger.debug(messageProvider())
58         }
59     }
60
61     fun debug(t: Throwable, messageProvider: () -> String) {
62         if (logger.isDebugEnabled) {
63             logger.debug(messageProvider(), t)
64         }
65     }
66
67
68     //
69     // INFO
70     //
71     fun info(message: String) {
72         logger.info(message)
73     }
74
75
76     fun info(messageProvider: () -> String) {
77         if (logger.isInfoEnabled) {
78             logger.info(messageProvider())
79         }
80     }
81
82     fun info(message: String, t: Throwable) {
83         logger.info(message, t)
84     }
85
86
87     fun info(t: Throwable, messageProvider: () -> String) {
88         if (logger.isInfoEnabled) {
89             logger.info(messageProvider(), t)
90         }
91     }
92
93
94     //
95     // WARN
96     //
97
98     fun warn(message: String) {
99         logger.warn(message)
100     }
101
102     fun warn(message: String, t: Throwable) {
103         logger.warn(message, t)
104     }
105
106     fun warn(messageProvider: () -> String) {
107         if (logger.isWarnEnabled) {
108             logger.warn(messageProvider())
109         }
110     }
111
112     fun warn(t: Throwable, messageProvider: () -> String) {
113         if (logger.isWarnEnabled) {
114             logger.warn(messageProvider(), t)
115         }
116     }
117
118
119     //
120     // ERROR
121     //
122
123     fun error(message: String) {
124         logger.error(message)
125     }
126
127
128     fun error(message: String, t: Throwable) {
129         logger.error(message, t)
130     }
131
132     fun error(messageProvider: () -> String) {
133         if (logger.isErrorEnabled) {
134             logger.error(messageProvider())
135         }
136     }
137
138     fun error(t: Throwable, messageProvider: () -> String) {
139         if (logger.isErrorEnabled) {
140             logger.error(messageProvider(), t)
141         }
142     }
143 }