diff --git a/src/main/java/com/rawlabs/utils/core/logger/RawLogger.java b/src/main/java/com/rawlabs/utils/core/logger/RawLogger.java new file mode 100644 index 0000000..6fc50c1 --- /dev/null +++ b/src/main/java/com/rawlabs/utils/core/logger/RawLogger.java @@ -0,0 +1,25 @@ +/* + * Copyright 2024 RAW Labs S.A. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.txt. + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0, included in the file + * licenses/APL.txt. + */ + +package com.rawlabs.utils.core.logger; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RawLogger { + + public final Logger logger; + + public RawLogger(Class clazz) { + logger = LoggerFactory.getLogger(clazz); + } +} diff --git a/src/main/java/com/rawlabs/utils/core/logger/RawLoggerWithTraceId.java b/src/main/java/com/rawlabs/utils/core/logger/RawLoggerWithTraceId.java new file mode 100644 index 0000000..8fb7b75 --- /dev/null +++ b/src/main/java/com/rawlabs/utils/core/logger/RawLoggerWithTraceId.java @@ -0,0 +1,20 @@ +/* + * Copyright 2024 RAW Labs S.A. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.txt. + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0, included in the file + * licenses/APL.txt. + */ + +package com.rawlabs.utils.core.logger; + +class RawLoggerWithTraceId extends RawLogger implements TraceIdLogging { + + public RawLoggerWithTraceId(Class clazz) { + super(clazz); + } +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 5dd09b9..4d8a4fd 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -20,7 +20,8 @@ requires org.apache.commons.io; requires org.apache.commons.text; requires com.google.common; + requires java.logging; exports com.rawlabs.utils.core; - + exports com.rawlabs.utils.core.logger; } diff --git a/src/main/scala/com/rawlabs/utils/core/ContextLogging.scala b/src/main/scala/com/rawlabs/utils/core/ContextLogging.scala new file mode 100644 index 0000000..44e5eb8 --- /dev/null +++ b/src/main/scala/com/rawlabs/utils/core/ContextLogging.scala @@ -0,0 +1,27 @@ +/* + * Copyright 2024 RAW Labs S.A. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.txt. + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0, included in the file + * licenses/APL.txt. + */ + +package com.rawlabs.utils.core + +import org.slf4j.MDC + +trait ContextLogging { + final def setContextValue(key: String, value: String): Unit = MDC.put(key, value) + + final def clearContextValue(key: String): Unit = MDC.remove(key) + + final def withContextValue[T](key: String, value: String)(f: => T): T = { + setContextValue(key, value) + try f + finally clearContextValue(key) + } +} diff --git a/src/main/scala/com/rawlabs/utils/core/logger/RawLogging.scala b/src/main/scala/com/rawlabs/utils/core/logger/RawLogging.scala new file mode 100644 index 0000000..7adfafe --- /dev/null +++ b/src/main/scala/com/rawlabs/utils/core/logger/RawLogging.scala @@ -0,0 +1,17 @@ +/* + * Copyright 2024 RAW Labs S.A. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.txt. + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0, included in the file + * licenses/APL.txt. + */ + +package com.rawlabs.utils.core.logger + +import com.typesafe.scalalogging.StrictLogging + +trait RawLogging extends StrictLogging {} diff --git a/src/main/scala/com/rawlabs/utils/core/logger/RawLoggingWithTraceId.scala b/src/main/scala/com/rawlabs/utils/core/logger/RawLoggingWithTraceId.scala new file mode 100644 index 0000000..a95dc5e --- /dev/null +++ b/src/main/scala/com/rawlabs/utils/core/logger/RawLoggingWithTraceId.scala @@ -0,0 +1,15 @@ +/* + * Copyright 2024 RAW Labs S.A. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.txt. + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0, included in the file + * licenses/APL.txt. + */ + +package com.rawlabs.utils.core.logger + +trait RawLoggingWithTraceId extends TraceIdLogging with RawLogging {} diff --git a/src/main/scala/com/rawlabs/utils/core/logger/TraceIdLogging.scala b/src/main/scala/com/rawlabs/utils/core/logger/TraceIdLogging.scala new file mode 100644 index 0000000..3dafa7a --- /dev/null +++ b/src/main/scala/com/rawlabs/utils/core/logger/TraceIdLogging.scala @@ -0,0 +1,29 @@ +/* + * Copyright 2024 RAW Labs S.A. + * + * Use of this software is governed by the Business Source License + * included in the file licenses/BSL.txt. + * + * As of the Change Date specified in that file, in accordance with + * the Business Source License, use of this software will be governed + * by the Apache License, Version 2.0, included in the file + * licenses/APL.txt. + */ + +package com.rawlabs.utils.core.logger + +import com.rawlabs.utils.core.ContextLogging + +private object TraceIdLogging { + private val TRACE_ID = "traceId" +} + +trait TraceIdLogging extends ContextLogging { + import TraceIdLogging._ + + final def setTraceId(traceId: String): Unit = setContextValue(TRACE_ID, traceId) + + final def clearTraceId(): Unit = clearContextValue(TRACE_ID) + + final def withTraceId[T](traceId: String)(f: => T): T = withContextValue(TRACE_ID, traceId)(f) +}