Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing universal logger #16

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
25 changes: 25 additions & 0 deletions src/main/java/com/rawlabs/utils/core/logger/RawLogger.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
27 changes: 27 additions & 0 deletions src/main/scala/com/rawlabs/utils/core/ContextLogging.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
17 changes: 17 additions & 0 deletions src/main/scala/com/rawlabs/utils/core/logger/RawLogging.scala
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
@@ -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 {}
29 changes: 29 additions & 0 deletions src/main/scala/com/rawlabs/utils/core/logger/TraceIdLogging.scala
Original file line number Diff line number Diff line change
@@ -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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one inherits the TraceIdLogging trait, it can set the trace ID with the dedicated methods (as the name indicates), but also has access to the inner ContextLogging methods that permit to add any random key to MDC. Just wondering if that could be a problem.

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)
}