Skip to content

Commit

Permalink
ConfigurableLogger - moved to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon committed Oct 28, 2023
1 parent 8e1d540 commit 2549bf3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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"))
}
}
}
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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"'
*/

0 comments on commit 2549bf3

Please sign in to comment.