diff options
Diffstat (limited to 'sources/hv-collector-utils/src')
3 files changed, 89 insertions, 20 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 1e5c9c55..938ba793 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 @@ -127,6 +127,7 @@ abstract class AtLevelLogger { abstract fun log(message: String) abstract fun log(message: String, t: Throwable) abstract fun log(marker: Marker, message: String) + open val enabled: Boolean get() = true @@ -140,6 +141,19 @@ abstract class AtLevelLogger { } } } + + protected fun withAdditionalMdc(mdc: Map<String, String>, block: () -> Unit) { + if (mdc.isEmpty()) { + block() + } else { + try { + mdc.forEach(MDC::put) + block() + } finally { + mdc.keys.forEach(MDC::remove) + } + } + } } object OffLevelLogger : AtLevelLogger() { @@ -168,9 +182,10 @@ class ErrorLevelLogger(private val logger: org.slf4j.Logger) : AtLevelLogger() { logger.error(message, t) } - override fun log(marker: Marker, message: String) { - logger.error(marker(), message) - } + override fun log(marker: Marker, message: String) = + withAdditionalMdc(marker.mdc) { + logger.error(marker.slf4jMarker, message) + } } @Suppress("SuboptimalLoggerUsage") @@ -183,9 +198,10 @@ class WarnLevelLogger(private val logger: org.slf4j.Logger) : AtLevelLogger() { logger.warn(message, t) } - override fun log(marker: Marker, message: String) { - logger.warn(marker(), message) - } + override fun log(marker: Marker, message: String) = + withAdditionalMdc(marker.mdc) { + logger.warn(marker.slf4jMarker, message) + } } @Suppress("SuboptimalLoggerUsage") @@ -198,9 +214,10 @@ class InfoLevelLogger(private val logger: org.slf4j.Logger) : AtLevelLogger() { logger.info(message, t) } - override fun log(marker: Marker, message: String) { - logger.info(marker(), message) - } + override fun log(marker: Marker, message: String) = + withAdditionalMdc(marker.mdc) { + logger.info(marker.slf4jMarker, message) + } } @Suppress("SuboptimalLoggerUsage") @@ -213,9 +230,10 @@ class DebugLevelLogger(private val logger: org.slf4j.Logger) : AtLevelLogger() { logger.debug(message, t) } - override fun log(marker: Marker, message: String) { - logger.debug(marker(), message) - } + override fun log(marker: Marker, message: String) = + withAdditionalMdc(marker.mdc) { + logger.debug(marker.slf4jMarker, message) + } } @Suppress("SuboptimalLoggerUsage") @@ -228,7 +246,8 @@ class TraceLevelLogger(private val logger: org.slf4j.Logger) : AtLevelLogger() { logger.trace(message, t) } - override fun log(marker: Marker, message: String) { - logger.trace(marker(), message) - } + override fun log(marker: Marker, message: String) = + withAdditionalMdc(marker.mdc) { + logger.trace(marker.slf4jMarker, message) + } } diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Marker.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Marker.kt index 83fb9a5e..9023528e 100644 --- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Marker.kt +++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Marker.kt @@ -20,11 +20,26 @@ package org.onap.dcae.collectors.veshv.utils.logging import org.slf4j.MarkerFactory +import java.time.Instant +import java.util.* -enum class Marker(private val marker: org.slf4j.Marker) { - ENTRY(MarkerFactory.getMarker("ENTRY")), - EXIT(MarkerFactory.getMarker("EXIT")), - INVOKE(MarkerFactory.getMarker("INVOKE")); +sealed class Marker(internal val slf4jMarker: org.slf4j.Marker, val mdc: Map<String, String> = emptyMap()) { - operator fun invoke() = marker + object Entry : Marker(ENTRY) + object Exit : Marker(EXIT) + + class Invoke(id: UUID = UUID.randomUUID(), timestamp: Instant = Instant.now()) : Marker(INVOKE, mdc(id, timestamp)) { + companion object { + private fun mdc(id: UUID, timestamp: Instant) = mapOf( + OnapMdc.INVOCATION_ID to id.toString(), + OnapMdc.INVOCATION_TIMESTAMP to timestamp.toString() + ) + } + } + + companion object { + private val ENTRY = MarkerFactory.getMarker("ENTRY") + private val EXIT = MarkerFactory.getMarker("EXIT") + private val INVOKE = MarkerFactory.getMarker("INVOKE") + } } diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/OnapMdc.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/OnapMdc.kt new file mode 100644 index 00000000..86584164 --- /dev/null +++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/OnapMdc.kt @@ -0,0 +1,35 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.utils.logging + +/** + * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> + * @since December 2018 + */ +object OnapMdc { + const val REQUEST_ID = "RequestID" + const val CLIENT_NAME = "PartnerName" + const val CLIENT_IP = "ClientIPAddress" + const val INVOCATION_ID = "InvocationID" + const val INVOCATION_TIMESTAMP = "InvokeTimestamp" + const val STATUS_CODE = "StatusCode" + const val INSTANCE_ID = "InstanceID" + const val SERVER_FQDN = "ServerFQDN" +} |