From 9058db7a33a33371eff0bd827533dfe4a7cec3ef Mon Sep 17 00:00:00 2001 From: Doron Somech Date: Wed, 9 Oct 2024 21:16:11 +0300 Subject: [PATCH 1/4] Add appendLoggerName aspect --- .../zio/logging/AppendLoggerNameSpec.scala | 19 +++++++++++++++++++ .../src/main/scala/zio/logging/package.scala | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala diff --git a/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala b/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala new file mode 100644 index 00000000..67645204 --- /dev/null +++ b/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala @@ -0,0 +1,19 @@ +package zio.logging + +import zio.test._ +import zio.logging.appendLoggerName +import zio.FiberRef + +object AppendLoggerNameSpec extends ZIOSpecDefault { + + val spec: Spec[Environment, Any] = suite("AppendLoggerNameSpec")( + test("appendLoggerName") { + for { + name <- FiberRef.currentLogAnnotations.get.map(annotations => + annotations.get(loggerNameAnnotationKey) + ) @@ appendLoggerName("logging") @@ appendLoggerName("zio") + + } yield assertTrue(name == Some("zio.logging")) + } + ) +} diff --git a/core/shared/src/main/scala/zio/logging/package.scala b/core/shared/src/main/scala/zio/logging/package.scala index 4e3e0c4a..6ca7fb5d 100644 --- a/core/shared/src/main/scala/zio/logging/package.scala +++ b/core/shared/src/main/scala/zio/logging/package.scala @@ -54,6 +54,23 @@ package object logging extends LoggerLayers { def loggerName(value: String): ZIOAspect[Nothing, Any, Nothing, Any, Nothing, Any] = 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. + * + * annotation key: [[zio.logging.loggerNameAnnotationKey]] + */ + def appendLoggerName(value: 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 <- FiberRef.currentLogAnnotations.get + currentLoggerName = annotations.get(loggerNameAnnotationKey) + newLoggerName = currentLoggerName.fold(value)(currentLoggerName => s"$currentLoggerName.$value") + a <- ZIO.logAnnotate(loggerNameAnnotationKey, newLoggerName)(zio) + } yield a + } + 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) From 7bec7fe823b75029142b07ff0d806ff8cfd6aa5f Mon Sep 17 00:00:00 2001 From: Doron Somech Date: Wed, 9 Oct 2024 22:26:09 +0300 Subject: [PATCH 2/4] use ZIO.logAnnotations --- .../src/test/scala/zio/logging/AppendLoggerNameSpec.scala | 8 ++++---- core/shared/src/main/scala/zio/logging/package.scala | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala b/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala index 67645204..6be5f0d8 100644 --- a/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala +++ b/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala @@ -2,16 +2,16 @@ package zio.logging import zio.test._ import zio.logging.appendLoggerName -import zio.FiberRef +import zio.ZIO object AppendLoggerNameSpec extends ZIOSpecDefault { val spec: Spec[Environment, Any] = suite("AppendLoggerNameSpec")( test("appendLoggerName") { for { - name <- FiberRef.currentLogAnnotations.get.map(annotations => - annotations.get(loggerNameAnnotationKey) - ) @@ appendLoggerName("logging") @@ appendLoggerName("zio") + name <- ZIO.logAnnotations.map(annotations => annotations.get(loggerNameAnnotationKey)) @@ appendLoggerName( + "logging" + ) @@ appendLoggerName("zio") } yield assertTrue(name == Some("zio.logging")) } diff --git a/core/shared/src/main/scala/zio/logging/package.scala b/core/shared/src/main/scala/zio/logging/package.scala index 6ca7fb5d..d0c0c5dd 100644 --- a/core/shared/src/main/scala/zio/logging/package.scala +++ b/core/shared/src/main/scala/zio/logging/package.scala @@ -64,7 +64,7 @@ package object logging extends LoggerLayers { 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 <- FiberRef.currentLogAnnotations.get + annotations <- ZIO.logAnnotations currentLoggerName = annotations.get(loggerNameAnnotationKey) newLoggerName = currentLoggerName.fold(value)(currentLoggerName => s"$currentLoggerName.$value") a <- ZIO.logAnnotate(loggerNameAnnotationKey, newLoggerName)(zio) From 3fc5d4e738a28120ed2e2c2355ed87ac04180425 Mon Sep 17 00:00:00 2001 From: Doron Somech Date: Wed, 9 Oct 2024 22:47:21 +0300 Subject: [PATCH 3/4] 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..4898f5b5 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(currentLoggerName => currentLoggerName.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) From cb17fb38d16f2e2f8fa55469bf57d5451db554db Mon Sep 17 00:00:00 2001 From: Doron Somech Date: Thu, 10 Oct 2024 20:25:13 +0300 Subject: [PATCH 4/4] fmt --- .../jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala b/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala index 6be5f0d8..a2d0ca21 100644 --- a/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala +++ b/core/jvm/src/test/scala/zio/logging/AppendLoggerNameSpec.scala @@ -1,8 +1,8 @@ package zio.logging -import zio.test._ -import zio.logging.appendLoggerName import zio.ZIO +import zio.logging.appendLoggerName +import zio.test._ object AppendLoggerNameSpec extends ZIOSpecDefault {