Skip to content

Added Logger that takes an implicit parameter (e.g. CorrelationId) #83

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

Merged
merged 10 commits into from
Feb 5, 2018
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,49 @@ class MyClass extends LazyLogging {
}
```

`LoggerTakingImplicit` provides the same methods as `Logger` class, but with additional implicit parameter `A`.
During creation of the `LoggerTakingImplicit` evidence `CanLog[A]` is required.
It may be useful when contextual parameter (e.g. _Correlation ID_) is being passed around and you would like to include it in the log messages:

```scala
case class CorrelationId(value: String)
implicit case object CanLogCorrelationId extends CanLog[CorrelationId] {
override def logMessage(originalMsg: String, a: CorrelationId): String = s"${a.value} $originalMsg"
}

implicit val correlationId = CorrelationId("ID")

val logger = Logger.takingImplicit[CorrelationId]("test")
logger.info("Test") // takes implicit correlationId and logs "ID Test"
```

It's possible to use `MDC` through `CanLog` without any troubles with execution context.

```scala
case class CorrelationId(value: String)
implicit case object CanLogCorrelationId extends CanLog[CorrelationId] {
override def logMessage(originalMsg: String, a: CorrelationId): String = {
MDC.put("correlationId", a.value)
originalMsg
}

override def afterLog(a: A): Unit = {
MDC.remove("correlationId")
}
}

implicit val correlationId = CorrelationId("ID")

val logger = Logger.takingImplicit[CorrelationId]("test")

def serviceMethod(implicit correlationId: CorrelationId): Future[Result] = {
dbCall.map { value =>
logger.trace(s"Received value $value from db") // takes implicit correlationId
toResult(value)
}
}
```

### What's new?

#### 3.7.2
Expand Down
33 changes: 33 additions & 0 deletions src/main/scala/com/typesafe/scalalogging/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ object Logger {
def apply(underlying: Underlying): Logger =
new Logger(underlying)

/**
* Create a [[LoggerTakingImplicit]] wrapping the given underlying `org.slf4j.Logger`.
*/
def takingImplicit[A](underlying: Underlying)(implicit ev: CanLog[A]): LoggerTakingImplicit[A] =
new LoggerTakingImplicit[A](underlying)

/**
* Create a [[Logger]] for the given name.
* Example:
Expand All @@ -41,12 +47,28 @@ object Logger {
def apply(name: String): Logger =
new Logger(LoggerFactory.getLogger(name))

/**
* Create a [[LoggerTakingImplicit]] for the given name.
* Example:
* {{{
* val logger = Logger.takingImplicit[CorrelationId]("application")
* }}}
*/
def takingImplicit[A](name: String)(implicit ev: CanLog[A]): LoggerTakingImplicit[A] =
new LoggerTakingImplicit[A](LoggerFactory.getLogger(name))

/**
* Create a [[Logger]] wrapping the created underlying `org.slf4j.Logger`.
*/
def apply(clazz: Class[_]): Logger =
new Logger(LoggerFactory.getLogger(clazz.getName))

/**
* Create a [[LoggerTakingImplicit]] wrapping the created underlying `org.slf4j.Logger`.
*/
def takingImplicit[A](clazz: Class[_])(implicit ev: CanLog[A]): LoggerTakingImplicit[A] =
new LoggerTakingImplicit[A](LoggerFactory.getLogger(clazz.getName))

/**
* Create a [[Logger]] for the runtime class wrapped by the implicit class
* tag parameter.
Expand All @@ -57,6 +79,17 @@ object Logger {
*/
def apply[T](implicit ct: ClassTag[T]): Logger =
new Logger(LoggerFactory.getLogger(ct.runtimeClass.getName.stripSuffix("$")))

/**
* Create a [[LoggerTakingImplicit]] for the runtime class wrapped by the implicit class
* tag parameter.
* Example:
* {{{
* val logger = Logger.takingImplicit[MyClass, CorrelationId]
* }}}
*/
def takingImplicit[T, A](implicit ct: ClassTag[T], ev: CanLog[A]): LoggerTakingImplicit[A] =
new LoggerTakingImplicit[A](LoggerFactory.getLogger(ct.runtimeClass.getName.stripSuffix("$")))
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.typesafe.scalalogging

import org.slf4j.{ Marker, Logger => Underlying }

trait CanLog[A] {
def logMessage(originalMsg: String, a: A): String
def afterLog(a: A): Unit = ()
}

@SerialVersionUID(957385465L)
Copy link
Collaborator

Choose a reason for hiding this comment

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

why a SerialVersionUID and why Serializable?

Copy link
Collaborator

Choose a reason for hiding this comment

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

let's remove this

Copy link
Author

Choose a reason for hiding this comment

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

Just SerialVersionUID or also Serializable?

Responding to an earlier question, it just mirrors whats in "regular" Logger + to allow Spark users to use it

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah ok makes sense

final class LoggerTakingImplicit[A] private[scalalogging] (val underlying: Underlying)(implicit val canLogEv: CanLog[A]) extends Serializable {

// Error

def error(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessage[A]

def error(message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageCause[A]

def error(message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageArgs[A]

def error(marker: Marker, message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageMarker[A]

def error(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageCauseMarker[A]

def error(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageArgsMarker[A]

// Warn

def warn(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessage[A]

def warn(message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageCause[A]

def warn(message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageArgs[A]

def warn(marker: Marker, message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageMarker[A]

def warn(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageCauseMarker[A]

def warn(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageArgsMarker[A]

// Info

def info(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessage[A]

def info(message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageCause[A]

def info(message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageArgs[A]

def info(marker: Marker, message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageMarker[A]

def info(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageCauseMarker[A]

def info(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageArgsMarker[A]

// Debug

def debug(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessage[A]

def debug(message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageCause[A]

def debug(message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageArgs[A]

def debug(marker: Marker, message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageMarker[A]

def debug(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageCauseMarker[A]

def debug(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageArgsMarker[A]

// Trace

def trace(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessage[A]

def trace(message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageCause[A]

def trace(message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageArgs[A]

def trace(marker: Marker, message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageMarker[A]

def trace(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageCauseMarker[A]

def trace(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageArgsMarker[A]

}
Loading