Skip to content

Commit

Permalink
add loggerName that accepts a function
Browse files Browse the repository at this point in the history
  • Loading branch information
somdoron committed Oct 9, 2024
1 parent 7bec7fe commit 5ea936c
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions core/shared/src/main/scala/zio/logging/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,30 @@ package object logging extends LoggerLayers {
ZIOAspect.annotated(loggerNameAnnotationKey, value)

/**
* Append logger name aspect, by which it is possible to append a logger name to the current logger name.
* The new name is appended using a dot (.) as the delimiter.
* Logger name aspect, by this aspect is possible to set logger name (in general, logger name is extracted from [[Trace]])
*
* annotation key: [[zio.logging.loggerNameAnnotationKey]]
*/
def appendLoggerName(value: String): ZIOAspect[Nothing, Any, Nothing, Any, Nothing, Any] =
def loggerName(fn: Option[String] => String): ZIOAspect[Nothing, Any, Nothing, Any, Nothing, Any] =
new ZIOAspect[Nothing, Any, Nothing, Any, Nothing, Any] {
def apply[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
for {
annotations <- ZIO.logAnnotations
currentLoggerName = annotations.get(loggerNameAnnotationKey)
newLoggerName = currentLoggerName.fold(value)(currentLoggerName => s"$currentLoggerName.$value")
newLoggerName = fn(currentLoggerName)
a <- ZIO.logAnnotate(loggerNameAnnotationKey, newLoggerName)(zio)
} yield a
}

/**
* Append logger name aspect, by which it is possible to append a logger name to the current logger name.
* The new name is appended using a dot (.) as the delimiter.
*
* annotation key: [[zio.logging.loggerNameAnnotationKey]]
*/
def appendLoggerName(value: String): ZIOAspect[Nothing, Any, Nothing, Any, Nothing, Any] =
loggerName(fn => fn.fold(value)(currentLoggerName => s"$currentLoggerName.$value"))

implicit final class LogAnnotationZIOSyntax[R, E, A](private val self: ZIO[R, E, A]) {
def logAnnotate[V: Tag](key: LogAnnotation[V], value: V): ZIO[R, E, A] =
self @@ key(value)
Expand Down

0 comments on commit 5ea936c

Please sign in to comment.