From 8b8c37c296e55644063e0332fd455437168e78da Mon Sep 17 00:00:00 2001 From: Piotr Jaszczyk Date: Fri, 7 Dec 2018 14:41:39 +0100 Subject: Add log diagnostic context As it's not trivial to use MDC directly from logging framework in reactive application, we need to do some work manually. The approach proposed is an explicit MDC handling, which means that context is kept as an object created after establishing client connection. Next, new instance of HvVesCollector (and its dependencies) is created. Every object is propagated with ClientContext so it can use it when calling logger methods. In the future ClientContext might be used to support other use-cases, ie. per-topic access control. As a by-product I had to refactor our Logger wrapper, too. It already had too many functions and after adding MDC number would be doubled. Change-Id: I9c5d3f5e1d1be1db66d28d292eb0e1c38d8d0ffe Issue-ID: DCAEGEN2-671 Signed-off-by: Piotr Jaszczyk --- .../src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt | 6 +++--- .../org/onap/dcae/collectors/veshv/main/servers/ServerStarter.kt | 2 +- sources/hv-collector-main/src/main/resources/logback.xml | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) (limited to 'sources/hv-collector-main') diff --git a/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt b/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt index 899f51fb..5c9566c7 100644 --- a/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt +++ b/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt @@ -40,15 +40,15 @@ fun main(args: Array) = .map(::startAndAwaitServers) .unsafeRunEitherSync( { ex -> - logger.error("Failed to start a server", ex) + logger.withError { log("Failed to start a server", ex) } ExitFailure(1) }, - { logger.info("Gentle shutdown") } + { logger.info { "Gentle shutdown" } } ) private fun startAndAwaitServers(config: ServerConfiguration) = IO.monad().binding { - logger.info("Using configuration: $config") + logger.info { "Using configuration: $config" } HealthCheckServer.start(config).bind() VesServer.start(config).bind() .await().bind() diff --git a/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/servers/ServerStarter.kt b/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/servers/ServerStarter.kt index 5c6f1277..13b0bc7b 100644 --- a/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/servers/ServerStarter.kt +++ b/sources/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/servers/ServerStarter.kt @@ -31,7 +31,7 @@ import org.onap.dcae.collectors.veshv.utils.logging.Logger abstract class ServerStarter { fun start(config: ServerConfiguration): IO = startServer(config) - .map { logger.info(serverStartedMessage(it)); it } + .map { logger.info { serverStartedMessage(it) }; it } protected abstract fun startServer(config: ServerConfiguration): IO protected abstract fun serverStartedMessage(handle: ServerHandle): String diff --git a/sources/hv-collector-main/src/main/resources/logback.xml b/sources/hv-collector-main/src/main/resources/logback.xml index bee0dae1..674fb2c3 100644 --- a/sources/hv-collector-main/src/main/resources/logback.xml +++ b/sources/hv-collector-main/src/main/resources/logback.xml @@ -12,6 +12,7 @@ %nopexception%50.50logger | %date{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC} | %highlight(%-5level) +| %mdc{clientId} %mdc{clientAddress} | %msg | %rootException | %thread%n"/> -- cgit 1.2.3-korg