From 2549bf340634084aa3c70ef54291c349679c52a8 Mon Sep 17 00:00:00 2001 From: Peter Kotula Date: Sun, 15 Oct 2023 19:53:26 +0200 Subject: [PATCH] ConfigurableLogger - moved to examples --- .../zio/logging/ConfigurableLogger.scala | 40 +++++-------------- .../example/ConfigurableLoggerApp.scala | 22 +++++----- 2 files changed, 19 insertions(+), 43 deletions(-) rename {core/shared => examples/core}/src/main/scala/zio/logging/ConfigurableLogger.scala (78%) diff --git a/core/shared/src/main/scala/zio/logging/ConfigurableLogger.scala b/examples/core/src/main/scala/zio/logging/ConfigurableLogger.scala similarity index 78% rename from core/shared/src/main/scala/zio/logging/ConfigurableLogger.scala rename to examples/core/src/main/scala/zio/logging/ConfigurableLogger.scala index 581bac78..6ff7d85d 100644 --- a/core/shared/src/main/scala/zio/logging/ConfigurableLogger.scala +++ b/examples/core/src/main/scala/zio/logging/ConfigurableLogger.scala @@ -15,7 +15,7 @@ */ package zio.logging -import zio.{ Cause, FiberId, FiberRef, FiberRefs, LogLevel, LogSpan, Trace, ZIO, ZLayer, ZLogger } +import zio.{ Cause, FiberId, FiberRefs, LogLevel, LogSpan, Trace, ZIO, ZLayer, ZLogger } trait LoggerConfigurer { @@ -44,16 +44,14 @@ object LoggerConfigurer { val layer: ZLayer[Any, Throwable, LoggerConfigurer] = ZLayer.fromZIO { - for { - fiberRefs <- ZIO.getFiberRefs - - loggerService <- ZIO.attempt { - val loggers = fiberRefs.getOrDefault(FiberRef.currentLoggers) - loggers.collectFirst { case logger: ConfigurableLogger[_, _] => - logger.configurer - }.getOrElse(throw new RuntimeException("LoggerConfigurer not found")) - } - } yield loggerService + ZIO.loggers.flatMap { loggers => + loggers.collectFirst { case logger: ConfigurableLogger[_, _] => + logger.configurer + } match { + case Some(value) => ZIO.succeed(value) + case None => ZIO.fail(new RuntimeException("LoggerConfigurer not found")) + } + } } } @@ -64,26 +62,6 @@ trait ConfigurableLogger[-Message, +Output] extends ZLogger[Message, Output] { object ConfigurableLogger { -// def apply[Message, Output]( -// logger: ZLogger[Message, Output], -// loggerConfigurer: LoggerConfigurer -// ): ConfigurableLogger[Message, Output] = -// new ConfigurableLogger[Message, Output] { -// -// override val configurer: LoggerConfigurer = loggerConfigurer -// -// override def apply( -// trace: Trace, -// fiberId: FiberId, -// logLevel: LogLevel, -// message: () => Message, -// cause: Cause[Any], -// context: FiberRefs, -// spans: List[LogSpan], -// annotations: Map[String, String] -// ): Output = logger.apply(trace, fiberId, logLevel, message, cause, context, spans, annotations) -// } - def make[Message, Output]( logger: ZLogger[Message, Output], filterConfig: LogFilter.LogLevelByNameConfig diff --git a/examples/core/src/main/scala/zio/logging/example/ConfigurableLoggerApp.scala b/examples/core/src/main/scala/zio/logging/example/ConfigurableLoggerApp.scala index 530860de..d7fbd2cd 100644 --- a/examples/core/src/main/scala/zio/logging/example/ConfigurableLoggerApp.scala +++ b/examples/core/src/main/scala/zio/logging/example/ConfigurableLoggerApp.scala @@ -30,6 +30,16 @@ import zio.{ ExitCode, Runtime, Scope, ZIO, ZIOAppDefault, _ } import java.util.UUID +/* + curl -u "admin:admin" 'http://localhost:8080/example/logger' + + curl -u "admin:admin" 'http://localhost:8080/example/logger/root' + + curl -u "admin:admin" --location --request PUT 'http://localhost:8080/example/logger/root' --header 'Content-Type: application/json' --data-raw '"WARN"' + + curl -u "admin:admin" --location --request PUT 'http://localhost:8080/example/logger/zio.logging.example' --header 'Content-Type: application/json' --data-raw '"WARN"' + + */ object ConfigurableLoggerApp extends ZIOAppDefault { def configurableLogger(configPath: NonEmptyChunk[String] = loggerConfigPath) = @@ -86,15 +96,3 @@ object ConfigurableLoggerApp extends ZIOAppDefault { } yield ExitCode.success).provide(LoggerConfigurer.layer ++ Server.default) } - -/* - - curl -u "admin:admin" 'http://localhost:8080/example/logger' - - curl -u "admin:admin" 'http://localhost:8080/example/logger/root' - - curl -u "admin:admin" --location --request PUT 'http://localhost:8080/example/logger/root' --header 'Content-Type: application/json' --data-raw '"WARN"' - - curl -u "admin:admin" --location --request PUT 'http://localhost:8080/example/logger/zio.logging.example' --header 'Content-Type: application/json' --data-raw '"WARN"' - - */