aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIzabela Zawadzka <izabela.zawadzka@nokia.com>2019-01-03 14:49:54 +0100
committerIzabela Zawadzka <izabela.zawadzka@nokia.com>2019-01-04 09:37:43 +0100
commitd8a04226845eb71bd8dad384ac4ccd98486d2029 (patch)
treec526280f3927d4da1c560f62779cc5ed415f41d3
parent3d3eff47f0f94176f351d05f7dca39957a0c3c8b (diff)
Change AtLevelLoggers to be initialized by lazy
Change-Id: I3e9565d432c6df8dbc357caa61c10204f69f2e2a Signed-off-by: Izabela Zawadzka <izabela.zawadzka@nokia.com> Issue-ID: DCAEGEN2-1053
-rw-r--r--sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt20
-rw-r--r--sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt24
2 files changed, 28 insertions, 16 deletions
diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt
index 82ce50a9..ade9b480 100644
--- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt
+++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt
@@ -31,11 +31,21 @@ class Logger(logger: org.slf4j.Logger) {
constructor(clazz: KClass<out Any>) : this(LoggerFactory.getLogger(clazz.java))
constructor(name: String) : this(LoggerFactory.getLogger(name))
- private val errorLogger = if (logger.isErrorEnabled) ErrorLevelLogger(logger) else OffLevelLogger
- private val warnLogger = if (logger.isWarnEnabled) WarnLevelLogger(logger) else OffLevelLogger
- private val infoLogger = if (logger.isInfoEnabled) InfoLevelLogger(logger) else OffLevelLogger
- private val debugLogger = if (logger.isDebugEnabled) DebugLevelLogger(logger) else OffLevelLogger
- private val traceLogger = if (logger.isTraceEnabled) TraceLevelLogger(logger) else OffLevelLogger
+ private val errorLogger by lazy {
+ if (logger.isErrorEnabled) ErrorLevelLogger(logger) else OffLevelLogger
+ }
+ private val warnLogger by lazy {
+ if (logger.isWarnEnabled) WarnLevelLogger(logger) else OffLevelLogger
+ }
+ private val infoLogger by lazy {
+ if (logger.isInfoEnabled) InfoLevelLogger(logger) else OffLevelLogger
+ }
+ private val debugLogger by lazy {
+ if (logger.isDebugEnabled) DebugLevelLogger(logger) else OffLevelLogger
+ }
+ private val traceLogger by lazy {
+ if (logger.isTraceEnabled) TraceLevelLogger(logger) else OffLevelLogger
+ }
// ERROR
diff --git a/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt b/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt
index 10fc8d8f..c4086496 100644
--- a/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt
+++ b/sources/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt
@@ -19,10 +19,7 @@
*/
package org.onap.dcae.collectors.veshv.utils.logging
-import com.nhaarman.mockitokotlin2.mock
-import com.nhaarman.mockitokotlin2.verify
-import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
-import com.nhaarman.mockitokotlin2.whenever
+import com.nhaarman.mockitokotlin2.*
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
@@ -34,13 +31,7 @@ import org.jetbrains.spek.api.dsl.it
object LoggerTest : Spek({
lateinit var slf4jLogger: org.slf4j.Logger
- fun cut() = Logger(slf4jLogger).also {
- verify(slf4jLogger).isTraceEnabled
- verify(slf4jLogger).isDebugEnabled
- verify(slf4jLogger).isInfoEnabled
- verify(slf4jLogger).isWarnEnabled
- verify(slf4jLogger).isErrorEnabled
- }
+ fun cut() = Logger(slf4jLogger)
beforeEachTest {
slf4jLogger = mock()
@@ -54,6 +45,17 @@ object LoggerTest : Spek({
val message = "sample message"
val exception = Exception("fail")
+ describe("before first use") {
+ it("should not initialize AtLeverLoggers") {
+ cut()
+ verify(slf4jLogger, never()).isTraceEnabled
+ verify(slf4jLogger, never()).isDebugEnabled
+ verify(slf4jLogger, never()).isInfoEnabled
+ verify(slf4jLogger, never()).isWarnEnabled
+ verify(slf4jLogger, never()).isErrorEnabled
+ }
+ }
+
describe("debug levels") {
describe("lazy logging message") {