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)