From 5ea936c6f7988778241f0a710588f98e58b7c402 Mon Sep 17 00:00:00 2001 From: Doron Somech Date: Wed, 9 Oct 2024 22:47:21 +0300 Subject: [PATCH] add loggerName that accepts a function --- .../src/main/scala/zio/logging/package.scala | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/core/shared/src/main/scala/zio/logging/package.scala b/core/shared/src/main/scala/zio/logging/package.scala index d0c0c5dd..64f1bff9 100644 --- a/core/shared/src/main/scala/zio/logging/package.scala +++ b/core/shared/src/main/scala/zio/logging/package.scala @@ -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)