Skip to content

Commit

Permalink
refactor: change dispatch signature
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed May 20, 2022
1 parent 079cbd8 commit 918d1ed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
'{}
Expand All @@ -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 {
'{}
Expand All @@ -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 {
'{}
Expand All @@ -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 {
'{}
Expand All @@ -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 {
'{}
Expand All @@ -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 {
'{}
}
Expand Down
24 changes: 15 additions & 9 deletions zio-http-logging/src/main/scala/zhttp/logging/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}

1 comment on commit 918d1ed

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 922569.21

Please sign in to comment.