zio-logging icon indicating copy to clipboard operation
zio-logging copied to clipboard

Provide empty logger as instance for .updateService

Open domdorn opened this issue 3 years ago • 1 comments

today I needed to deactivate logging for a certain effect and noticed that we only have the Logging.ignore layer but not a simple EmptyLogger instance I can use with .updateService

import zio.{UIO, ZIO}
import zio.logging.{LogContext, Logger}

object EmptyLogger extends Logger[String] {
  override def locally[R1, E, A1](f: LogContext => LogContext)(in: ZIO[R1, E, A1]): ZIO[R1, E, A1] = in
  override def log(line: => String): UIO[Unit]                                                     = ZIO.unit
  override def logContext: UIO[LogContext]                                                         = ZIO.succeed(LogContext.empty)
}

I use it in my streams like this:

cockpitZR <-
                ZStream
                  .fromEffect(
                    (for {
                      result <- effectWithDisabledLogging(....)
                        .updateService[zio.logging.Logger[String]](_ => EmptyLogger)
                      _ <- someOtherEffectWithNormalLogging(...)
                    } yield result)
                      .tapCause(e => zio.logging.log.error("failed to calculate ...", e))
                  )

Maybe we can include this in a straightforward way into the library?

domdorn avatar Aug 04 '21 16:08 domdorn

that's good idea however I would probably use different name something like

import zio.{UIO, ZIO}
import zio.logging.{LogContext, Logger}

object IgnoreLogger extends Logger[String] {
  override def locally[R1, E, A1](f: LogContext => LogContext)(in: ZIO[R1, E, A1]): ZIO[R1, E, A1] = in
  override def log(line: => String): UIO[Unit]                                                     = ZIO.unit
  override def logContext: UIO[LogContext]                                                         = ZIO.succeed(LogContext.empty)
}

feel free to open open PR

pshemass avatar Aug 09 '21 15:08 pshemass

with zio 2, Runtime.removeDefaultLoggers should remove all loggers

closing this ticket as it was related to zio 1 implementation, fill free to open new one, if there are some other issues

justcoon avatar Oct 27 '22 12:10 justcoon