Skip to content

Commit

Permalink
Add strict evaluation option
Browse files Browse the repository at this point in the history
  • Loading branch information
ant8e committed Mar 25, 2023
1 parent fc595b0 commit 6a6bb28
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 6a6bb28

Please sign in to comment.