Creation of server module
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-domain / src / main / kotlin / org / onap / dcae / collectors / veshv / domain / logging / MarkerLogging.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2019 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.domain.logging
21
22 import org.onap.dcae.collectors.veshv.utils.logging.Logger
23 import org.onap.dcae.collectors.veshv.utils.logging.MappedDiagnosticContext
24 import org.slf4j.MDC
25
26
27 @Suppress("TooManyFunctions")
28 object MarkerLogging {
29     fun Logger.error(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String) =
30             withError(mdc) { withAdditionalMdc(marker.mdc) { log(marker.slf4jMarker, message()) } }
31
32     fun Logger.error(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String, t: Throwable) =
33             withError(mdc) { withAdditionalMdc(marker.mdc) { log(marker.slf4jMarker, message(), t) } }
34
35     fun Logger.warn(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String) =
36             withWarn(mdc) { withAdditionalMdc(marker.mdc) { log(marker.slf4jMarker, message()) } }
37
38     fun Logger.warn(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String, t: Throwable) =
39             withWarn(mdc) { withAdditionalMdc(marker.mdc) { log(marker.slf4jMarker, message(), t) } }
40
41     fun Logger.info(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String) =
42             withInfo(mdc) { withAdditionalMdc(marker.mdc) { log(marker.slf4jMarker, message()) } }
43
44     fun Logger.debug(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String) =
45             withDebug(mdc) { withAdditionalMdc(marker.mdc) { log(marker.slf4jMarker, message()) } }
46
47     fun Logger.trace(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String) =
48             withTrace(mdc) { withAdditionalMdc(marker.mdc) { log(marker.slf4jMarker, message()) } }
49
50
51     private inline fun withAdditionalMdc(mdc: Map<String, String>, block: () -> Unit) {
52         if (mdc.isEmpty()) {
53             block()
54         } else {
55             try {
56                 mdc.forEach(MDC::put)
57                 block()
58             } finally {
59                 mdc.keys.forEach(MDC::remove)
60             }
61         }
62     }
63 }