From 6a6bb285f5abd1d561dbdcc5ba35707a795dcef9 Mon Sep 17 00:00:00 2001 From: Antoine Comte Date: Fri, 24 Mar 2023 18:50:12 +0100 Subject: [PATCH 1/3] Add strict evaluation option --- .../typelevel/log4cats/noop/NoOpFactory.scala | 20 +++-- .../typelevel/log4cats/noop/NoOpLogger.scala | 80 +++++++++++-------- 2 files changed, 59 insertions(+), 41 deletions(-) diff --git a/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpFactory.scala b/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpFactory.scala index 5d3e191e..61d8f0b8 100644 --- a/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpFactory.scala +++ b/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpFactory.scala @@ -22,13 +22,19 @@ import cats.Applicative object NoOpFactory extends LoggerFactoryGenCompanion { def apply[F[_]: Applicative]: LoggerFactory[F] = impl[F] - def impl[F[_]](implicit F: Applicative[F]): LoggerFactory[F] = new LoggerFactory[F] { - override def getLoggerFromName(name: String): SelfAwareStructuredLogger[F] = { - val _ = name - NoOpLogger.impl[F] + def strictEvalArgs[F[_]: Applicative]: LoggerFactory[F] = impl_[F](evaluateArgs = true) + + def impl[F[_]](implicit F: Applicative[F]): LoggerFactory[F] = impl_[F](evaluateArgs = false) + + private def impl_[F[_]](evaluateArgs: Boolean)(implicit F: Applicative[F]): LoggerFactory[F] = + new LoggerFactory[F] { + override def getLoggerFromName(name: String): SelfAwareStructuredLogger[F] = { + val _ = name + NoOpLogger.impl_[F](evaluateArgs) + } + + override def fromName(name: String): F[SelfAwareStructuredLogger[F]] = + F.pure(getLoggerFromName(name)) } - override def fromName(name: String): F[SelfAwareStructuredLogger[F]] = - F.pure(getLoggerFromName(name)) - } } diff --git a/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala b/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala index 7050b55e..fe783e28 100644 --- a/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala +++ b/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala @@ -21,39 +21,51 @@ import org.typelevel.log4cats.SelfAwareStructuredLogger object NoOpLogger { def apply[F[_]: Applicative]: SelfAwareStructuredLogger[F] = impl[F] - def impl[F[_]: Applicative]: SelfAwareStructuredLogger[F] = new SelfAwareStructuredLogger[F] { - val no: F[Boolean] = Applicative[F].pure(false) - val unit: F[Unit] = Applicative[F].pure(()) - @inline override def isTraceEnabled: F[Boolean] = no - @inline override def isDebugEnabled: F[Boolean] = no - @inline override def isInfoEnabled: F[Boolean] = no - @inline override def isWarnEnabled: F[Boolean] = no - @inline override def isErrorEnabled: F[Boolean] = no - @inline override def trace(t: Throwable)(msg: => String): F[Unit] = unit - @inline override def trace(msg: => String): F[Unit] = unit - @inline override def trace(ctx: Map[String, String])(msg: => String): F[Unit] = unit - @inline override def debug(t: Throwable)(msg: => String): F[Unit] = unit - @inline override def debug(msg: => String): F[Unit] = unit - @inline override def debug(ctx: Map[String, String])(msg: => String): F[Unit] = unit - @inline override def info(t: Throwable)(msg: => String): F[Unit] = unit - @inline override def info(msg: => String): F[Unit] = unit - @inline override def info(ctx: Map[String, String])(msg: => String): F[Unit] = unit - @inline override def warn(t: Throwable)(msg: => String): F[Unit] = unit - @inline override def warn(msg: => String): F[Unit] = unit - @inline override def warn(ctx: Map[String, String])(msg: => String): F[Unit] = unit - @inline override def error(t: Throwable)(msg: => String): F[Unit] = unit - @inline override def error(msg: => String): F[Unit] = unit - @inline override def error(ctx: Map[String, String])(msg: => String): F[Unit] = unit - @inline override def trace(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = - unit - @inline override def debug(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = - unit - @inline override def info(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = - unit - @inline override def warn(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = - unit - @inline override def error(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = - unit - } + def strictEvalArgs[F[_]: Applicative]: SelfAwareStructuredLogger[F] = + impl_[F](evaluateArgs = true) + + def impl[F[_]: Applicative] = impl_[F](evaluateArgs = false) + + private[noop] def impl_[F[_]: Applicative](evaluateArgs: Boolean): SelfAwareStructuredLogger[F] = + new SelfAwareStructuredLogger[F] { + + val no: F[Boolean] = Applicative[F].pure(false) + def void(arg: => Any): F[Unit] = Applicative[F].pure { + if (evaluateArgs) { + val _ = arg + () + } else () + } + @inline override def isTraceEnabled: F[Boolean] = no + @inline override def isDebugEnabled: F[Boolean] = no + @inline override def isInfoEnabled: F[Boolean] = no + @inline override def isWarnEnabled: F[Boolean] = no + @inline override def isErrorEnabled: F[Boolean] = no + @inline override def trace(t: Throwable)(msg: => String): F[Unit] = void(t) + @inline override def trace(msg: => String): F[Unit] = void(msg) + @inline override def trace(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) + @inline override def debug(t: Throwable)(msg: => String): F[Unit] = void(msg) + @inline override def debug(msg: => String): F[Unit] = void(msg) + @inline override def debug(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) + @inline override def info(t: Throwable)(msg: => String): F[Unit] = void(msg) + @inline override def info(msg: => String): F[Unit] = void(msg) + @inline override def info(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) + @inline override def warn(t: Throwable)(msg: => String): F[Unit] = void(msg) + @inline override def warn(msg: => String): F[Unit] = void(msg) + @inline override def warn(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) + @inline override def error(t: Throwable)(msg: => String): F[Unit] = void(msg) + @inline override def error(msg: => String): F[Unit] = void(msg) + @inline override def error(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) + @inline override def trace(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + void(msg) + @inline override def debug(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + void(msg) + @inline override def info(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + void(msg) + @inline override def warn(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + void(msg) + @inline override def error(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + void(msg) + } } From 373a21315ee52db25c9128c393507854d65b91c9 Mon Sep 17 00:00:00 2001 From: Antoine Comte Date: Sun, 26 Mar 2023 12:17:47 +0200 Subject: [PATCH 2/3] Separate lazy and strict implementation --- .../typelevel/log4cats/noop/NoOpLogger.scala | 115 ++++++++++++------ 1 file changed, 77 insertions(+), 38 deletions(-) diff --git a/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala b/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala index fe783e28..3c2ea08c 100644 --- a/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala +++ b/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala @@ -28,44 +28,83 @@ object NoOpLogger { def impl[F[_]: Applicative] = impl_[F](evaluateArgs = false) private[noop] def impl_[F[_]: Applicative](evaluateArgs: Boolean): SelfAwareStructuredLogger[F] = - new SelfAwareStructuredLogger[F] { + if (evaluateArgs) stricImpl else lazyImpl - val no: F[Boolean] = Applicative[F].pure(false) - def void(arg: => Any): F[Unit] = Applicative[F].pure { - if (evaluateArgs) { - val _ = arg - () - } else () - } - @inline override def isTraceEnabled: F[Boolean] = no - @inline override def isDebugEnabled: F[Boolean] = no - @inline override def isInfoEnabled: F[Boolean] = no - @inline override def isWarnEnabled: F[Boolean] = no - @inline override def isErrorEnabled: F[Boolean] = no - @inline override def trace(t: Throwable)(msg: => String): F[Unit] = void(t) - @inline override def trace(msg: => String): F[Unit] = void(msg) - @inline override def trace(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) - @inline override def debug(t: Throwable)(msg: => String): F[Unit] = void(msg) - @inline override def debug(msg: => String): F[Unit] = void(msg) - @inline override def debug(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) - @inline override def info(t: Throwable)(msg: => String): F[Unit] = void(msg) - @inline override def info(msg: => String): F[Unit] = void(msg) - @inline override def info(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) - @inline override def warn(t: Throwable)(msg: => String): F[Unit] = void(msg) - @inline override def warn(msg: => String): F[Unit] = void(msg) - @inline override def warn(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) - @inline override def error(t: Throwable)(msg: => String): F[Unit] = void(msg) - @inline override def error(msg: => String): F[Unit] = void(msg) - @inline override def error(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) - @inline override def trace(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = - void(msg) - @inline override def debug(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = - void(msg) - @inline override def info(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = - void(msg) - @inline override def warn(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = - void(msg) - @inline override def error(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = - void(msg) + private def lazyImpl[F[_]: Applicative] = new SelfAwareStructuredLogger[F] { + + val no: F[Boolean] = Applicative[F].pure(false) + val unit: F[Unit] = Applicative[F].pure(()) + + @inline override def isTraceEnabled: F[Boolean] = no + @inline override def isDebugEnabled: F[Boolean] = no + @inline override def isInfoEnabled: F[Boolean] = no + @inline override def isWarnEnabled: F[Boolean] = no + @inline override def isErrorEnabled: F[Boolean] = no + @inline override def trace(t: Throwable)(msg: => String): F[Unit] = unit + @inline override def trace(msg: => String): F[Unit] = unit + @inline override def trace(ctx: Map[String, String])(msg: => String): F[Unit] = unit + @inline override def debug(t: Throwable)(msg: => String): F[Unit] = unit + @inline override def debug(msg: => String): F[Unit] = unit + @inline override def debug(ctx: Map[String, String])(msg: => String): F[Unit] = unit + @inline override def info(t: Throwable)(msg: => String): F[Unit] = unit + @inline override def info(msg: => String): F[Unit] = unit + @inline override def info(ctx: Map[String, String])(msg: => String): F[Unit] = unit + @inline override def warn(t: Throwable)(msg: => String): F[Unit] = unit + @inline override def warn(msg: => String): F[Unit] = unit + @inline override def warn(ctx: Map[String, String])(msg: => String): F[Unit] = unit + @inline override def error(t: Throwable)(msg: => String): F[Unit] = unit + @inline override def error(msg: => String): F[Unit] = unit + @inline override def error(ctx: Map[String, String])(msg: => String): F[Unit] = unit + @inline override def trace(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + unit + @inline override def debug(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + unit + @inline override def info(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + unit + @inline override def warn(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + unit + @inline override def error(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + unit + } + + private def stricImpl[F[_]: Applicative] = new SelfAwareStructuredLogger[F] { + + val no: F[Boolean] = Applicative[F].pure(false) + def void(arg: => Any): F[Unit] = Applicative[F].pure { + val _ = arg + () } + + @inline override def isTraceEnabled: F[Boolean] = no + @inline override def isDebugEnabled: F[Boolean] = no + @inline override def isInfoEnabled: F[Boolean] = no + @inline override def isWarnEnabled: F[Boolean] = no + @inline override def isErrorEnabled: F[Boolean] = no + @inline override def trace(t: Throwable)(msg: => String): F[Unit] = void(t) + @inline override def trace(msg: => String): F[Unit] = void(msg) + @inline override def trace(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) + @inline override def debug(t: Throwable)(msg: => String): F[Unit] = void(msg) + @inline override def debug(msg: => String): F[Unit] = void(msg) + @inline override def debug(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) + @inline override def info(t: Throwable)(msg: => String): F[Unit] = void(msg) + @inline override def info(msg: => String): F[Unit] = void(msg) + @inline override def info(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) + @inline override def warn(t: Throwable)(msg: => String): F[Unit] = void(msg) + @inline override def warn(msg: => String): F[Unit] = void(msg) + @inline override def warn(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) + @inline override def error(t: Throwable)(msg: => String): F[Unit] = void(msg) + @inline override def error(msg: => String): F[Unit] = void(msg) + @inline override def error(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg) + @inline override def trace(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + void(msg) + @inline override def debug(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + void(msg) + @inline override def info(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + void(msg) + @inline override def warn(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + void(msg) + @inline override def error(ctx: Map[String, String], t: Throwable)(msg: => String): F[Unit] = + void(msg) + } + } From 8bf252741cb3296e847a127823b48e100a1f7042 Mon Sep 17 00:00:00 2001 From: Antoine Comte Date: Mon, 27 Mar 2023 08:54:49 +0200 Subject: [PATCH 3/3] Show all log levels enabled in strict implementation & fix typo --- .../org/typelevel/log4cats/noop/NoOpLogger.scala | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala b/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala index 3c2ea08c..95762949 100644 --- a/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala +++ b/noop/shared/src/main/scala/org/typelevel/log4cats/noop/NoOpLogger.scala @@ -28,7 +28,7 @@ object NoOpLogger { def impl[F[_]: Applicative] = impl_[F](evaluateArgs = false) private[noop] def impl_[F[_]: Applicative](evaluateArgs: Boolean): SelfAwareStructuredLogger[F] = - if (evaluateArgs) stricImpl else lazyImpl + if (evaluateArgs) strictImpl else lazyImpl private def lazyImpl[F[_]: Applicative] = new SelfAwareStructuredLogger[F] { @@ -67,19 +67,19 @@ object NoOpLogger { unit } - private def stricImpl[F[_]: Applicative] = new SelfAwareStructuredLogger[F] { + private def strictImpl[F[_]: Applicative] = new SelfAwareStructuredLogger[F] { - val no: F[Boolean] = Applicative[F].pure(false) + val yes: F[Boolean] = Applicative[F].pure(true) def void(arg: => Any): F[Unit] = Applicative[F].pure { val _ = arg () } - @inline override def isTraceEnabled: F[Boolean] = no - @inline override def isDebugEnabled: F[Boolean] = no - @inline override def isInfoEnabled: F[Boolean] = no - @inline override def isWarnEnabled: F[Boolean] = no - @inline override def isErrorEnabled: F[Boolean] = no + @inline override def isTraceEnabled: F[Boolean] = yes + @inline override def isDebugEnabled: F[Boolean] = yes + @inline override def isInfoEnabled: F[Boolean] = yes + @inline override def isWarnEnabled: F[Boolean] = yes + @inline override def isErrorEnabled: F[Boolean] = yes @inline override def trace(t: Throwable)(msg: => String): F[Unit] = void(t) @inline override def trace(msg: => String): F[Unit] = void(msg) @inline override def trace(ctx: Map[String, String])(msg: => String): F[Unit] = void(msg)