X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=sources%2Fhv-collector-utils%2Fsrc%2Fmain%2Fkotlin%2Forg%2Fonap%2Fdcae%2Fcollectors%2Fveshv%2Futils%2Flogging%2Freactive_logging.kt;h=99ecfd743d2b53a26ca5bc0392a37872ecc8182b;hb=HEAD;hp=95590d9d90dd26d0d0821368c51d33386fd55e04;hpb=da8cdb5ead509c376ef5f8445af6ae045139ab14;p=dcaegen2%2Fcollectors%2Fhv-ves.git diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/reactive_logging.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/reactive_logging.kt index 95590d9d..7d92ddaf 100644 --- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/reactive_logging.kt +++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/reactive_logging.kt @@ -25,6 +25,8 @@ import arrow.core.Try import reactor.core.publisher.Flux import reactor.core.publisher.Mono +typealias MessageEither = Either<() -> String, () -> String> + fun Logger.handleReactiveStreamError( context: MappedDiagnosticContext, ex: Throwable, @@ -60,7 +62,7 @@ fun Option.filterEmptyWithLog(logger: Logger, fun Flux.filterFailedWithLog(logger: Logger, context: MappedDiagnosticContext, - predicate: (T) -> Either<() -> String, () -> String>) = + predicate: (T) -> MessageEither): Flux = flatMap { t -> predicate(t).fold({ logger.warn(context, it) @@ -70,3 +72,19 @@ fun Flux.filterFailedWithLog(logger: Logger, Mono.just(t) }) } + + +fun Mono.onErrorLog(logger: Logger, + mdc: () -> Map, + msg: () -> String) = + doOnError { logException(logger, mdc, msg, it) } + +fun Flux.onErrorLog(logger: Logger, + mdc: () -> Map, + msg: () -> String) = + doOnError { logException(logger, mdc, msg, it) } + +private fun logException(logger: Logger, mdc: () -> Map, msg: () -> String, it: Throwable) { + logger.error(mdc) { "${msg()}: ${it.message}" } + logger.debug(mdc) { "Detailed stack trace: ${it}" } +}