diff --git a/zio-http-logging/src/main/scala-2/zhttp/logging/macros/LoggerMacroImpl.scala b/zio-http-logging/src/main/scala-2/zhttp/logging/macros/LoggerMacroImpl.scala index 0acc6d0653..b908deea6a 100644 --- a/zio-http-logging/src/main/scala-2/zhttp/logging/macros/LoggerMacroImpl.scala +++ b/zio-http-logging/src/main/scala-2/zhttp/logging/macros/LoggerMacroImpl.scala @@ -36,7 +36,7 @@ private[zhttp] object LoggerMacroImpl { q""" if($isEnabled) { val logMsg = ${msg.tree} - ${c.prefix.tree}.dispatch(logMsg, $error, $level, ${sourceLocation}) + ${c.prefix.tree}.dispatch(logMsg, $level, $error, ${sourceLocation}) } """ else q"()" diff --git a/zio-http-logging/src/main/scala-3/zhttp/logging/macros/LoggerMacroImpl.scala b/zio-http-logging/src/main/scala-3/zhttp/logging/macros/LoggerMacroImpl.scala index 6eebf40521..0c1d8f1a8e 100644 --- a/zio-http-logging/src/main/scala-3/zhttp/logging/macros/LoggerMacroImpl.scala +++ b/zio-http-logging/src/main/scala-3/zhttp/logging/macros/LoggerMacroImpl.scala @@ -22,7 +22,7 @@ private[zhttp] object LoggerMacroImpl { if(LogLevel.Trace >= Logger.detectedLevel) { val pos = sourcePos(using qctx) '{ - if ($logger.isTraceEnabled) $logger.dispatch($msg, None, LogLevel.Trace, Some($pos)) + if ($logger.isTraceEnabled) $logger.dispatch($msg, LogLevel.Trace, None, Some($pos)) } } else { '{} @@ -32,7 +32,7 @@ private[zhttp] object LoggerMacroImpl { def logDebugImpl(logger: Expr[Logger], msg: Expr[String])(using qctx: Quotes): quoted.Expr[Any] = if(LogLevel.Debug >= Logger.detectedLevel) { '{ - if ($logger.isDebugEnabled) $logger.dispatch($msg, None, LogLevel.Debug, None) + if ($logger.isDebugEnabled) $logger.dispatch($msg, LogLevel.Debug, None, None) } }else { '{} @@ -41,7 +41,7 @@ private[zhttp] object LoggerMacroImpl { def logInfoImpl(logger: Expr[Logger], msg: Expr[String])(using qctx: Quotes): quoted.Expr[Any] = if(LogLevel.Info >= Logger.detectedLevel) { '{ - if ($logger.isInfoEnabled) $logger.dispatch($msg, None, LogLevel.Info, None) + if ($logger.isInfoEnabled) $logger.dispatch($msg, LogLevel.Info, None, None) } }else { '{} @@ -50,7 +50,7 @@ private[zhttp] object LoggerMacroImpl { def logWarnImpl(logger: Expr[Logger], msg: Expr[String])(using qctx: Quotes): quoted.Expr[Any] = if(LogLevel.Warn >= Logger.detectedLevel) { '{ - if ($logger.isWarnEnabled) $logger.dispatch($msg, None, LogLevel.Warn, None) + if ($logger.isWarnEnabled) $logger.dispatch($msg, LogLevel.Warn, None, None) } }else { '{} @@ -59,7 +59,7 @@ private[zhttp] object LoggerMacroImpl { def logErrorWithCauseImpl(logger: Expr[Logger], t: Expr[Throwable], msg: Expr[String])(using qctx: Quotes): quoted.Expr[Any] = if(LogLevel.Error >= Logger.detectedLevel) { '{ - if ($logger.isErrorEnabled) $logger.dispatch($msg, Some($t), LogLevel.Error, None) + if ($logger.isErrorEnabled) $logger.dispatch($msg, LogLevel.Error, Some($t), None) } }else { '{} @@ -68,7 +68,7 @@ private[zhttp] object LoggerMacroImpl { def logErrorImpl(logger: Expr[Logger], msg: Expr[String])(using qctx: Quotes): quoted.Expr[Any] = if(LogLevel.Error >= Logger.detectedLevel) { - '{if ($logger.isErrorEnabled) $logger.dispatch($msg, None, LogLevel.Error, None)} + '{if ($logger.isErrorEnabled) $logger.dispatch($msg, LogLevel.Error, None, None)} }else { '{} } diff --git a/zio-http-logging/src/main/scala/zhttp/logging/Logger.scala b/zio-http-logging/src/main/scala/zhttp/logging/Logger.scala index 865dadb7db..8db9a27785 100644 --- a/zio-http-logging/src/main/scala/zhttp/logging/Logger.scala +++ b/zio-http-logging/src/main/scala/zhttp/logging/Logger.scala @@ -14,6 +14,13 @@ import java.nio.file.Path */ final case class Logger(transports: List[LoggerTransport]) extends LoggerMacroExtensions { self => + val isEnabled: Boolean = transports.exists(_.level != LogLevel.Disable) + val isDebugEnabled: Boolean = isEnabled && transports.exists(_.isDebugEnabled) + val isErrorEnabled: Boolean = isEnabled && transports.exists(_.isErrorEnabled) + val isInfoEnabled: Boolean = isEnabled && transports.exists(_.isInfoEnabled) + val isTraceEnabled: Boolean = isEnabled && transports.exists(_.isTraceEnabled) + val isWarnEnabled: Boolean = isEnabled && transports.exists(_.isWarnEnabled) + /** * Modifies each transport */ @@ -46,17 +53,16 @@ final case class Logger(transports: List[LoggerTransport]) extends LoggerMacroEx */ def dispatch( msg: String, - cause: Option[Throwable], level: LogLevel, + cause: Option[Throwable], sourceLocation: Option[SourcePos], ): Unit = transports.foreach(_.log(msg, cause, level, sourceLocation)) - val isEnabled: Boolean = transports.exists(_.level != LogLevel.Disable) - val isDebugEnabled: Boolean = isEnabled && transports.exists(_.isDebugEnabled) - val isErrorEnabled: Boolean = isEnabled && transports.exists(_.isErrorEnabled) - val isInfoEnabled: Boolean = isEnabled && transports.exists(_.isInfoEnabled) - val isTraceEnabled: Boolean = isEnabled && transports.exists(_.isTraceEnabled) - val isWarnEnabled: Boolean = isEnabled && transports.exists(_.isWarnEnabled) + /** + * Dispatches the parameters to all the transports. Internally invoked by the + * macro. + */ + def dispatch(msg: String, level: LogLevel): Unit = dispatch(msg, level, None, None) /** * Creates a new logger that will log messages that start with the given @@ -99,6 +105,8 @@ final case class Logger(transports: List[LoggerTransport]) extends LoggerMacroEx } object Logger { + private[zhttp] val detectedLevel: LogLevel = LogLevel.detectFromProps("ZHttpLogLevel").getOrElse(LogLevel.Error) + def apply(transport: LoggerTransport): Logger = Logger(List(transport)) def console: Logger = Logger(List(LoggerTransport.console)) @@ -108,6 +116,4 @@ object Logger { def make: Logger = Logger(Nil) final case class SourcePos(file: String, line: Int) - - private[zhttp] val detectedLevel: LogLevel = LogLevel.detectFromProps("ZHttpLogLevel").getOrElse(LogLevel.Error) }