diff --git a/zio-http-logging/src/main/scala-2/zhttp/logging/Logger.scala b/zio-http-logging/src/main/scala-2/zhttp/logging/Logger.scala index d1f0288f87..0a850745f6 100644 --- a/zio-http-logging/src/main/scala-2/zhttp/logging/Logger.scala +++ b/zio-http-logging/src/main/scala-2/zhttp/logging/Logger.scala @@ -1,7 +1,6 @@ package zhttp.logging import zhttp.logging.frontend.ConsoleLogger -import zhttp.logging.macros.LoggerMacro import zhttp.logging.macros.LoggerMacro._ final class Logger(configuration: Configuration) { @@ -34,8 +33,6 @@ final class Logger(configuration: Configuration) { } object Logger { - import scala.language.experimental.macros - final def getLogger: Logger = macro LoggerMacro.getLoggerImpl final def getLogger(logLevel: LogLevel) = new Logger(configuration = Configuration(getClass.getSimpleName, logLevel, LogFormat.default)) final def getLogger(loggerName: String) = diff --git a/zio-http-logging/src/main/scala-2/zhttp/logging/macros/LoggerMacro.scala b/zio-http-logging/src/main/scala-2/zhttp/logging/macros/LoggerMacro.scala index bdb0727b67..aedc8c2a8f 100644 --- a/zio-http-logging/src/main/scala-2/zhttp/logging/macros/LoggerMacro.scala +++ b/zio-http-logging/src/main/scala-2/zhttp/logging/macros/LoggerMacro.scala @@ -3,93 +3,13 @@ package zhttp.logging.macros import zhttp.logging.LogLevel.{DEBUG, ERROR, INFO, TRACE, WARN} import zhttp.logging.{LogLevel, Logger} -import scala.annotation.tailrec -import scala.reflect.macros.{blackbox, whitebox} +import scala.reflect.macros.whitebox /** * Macro inspired from log4s. */ private[zhttp] object LoggerMacro { - /** Get a logger by reflecting the enclosing class name. */ - final def getLoggerImpl(c: blackbox.Context) = { - import c.universe._ - - @tailrec def findEnclosingClass(sym: c.universe.Symbol): c.universe.Symbol = { - sym match { - case NoSymbol => - c.abort(c.enclosingPosition, s"Couldn't find an enclosing class or module for the logger") - case s if s.isModule || s.isClass => - s - case other => - /* We're not in a module or a class, so we're probably inside a member definition. Recurse upward. */ - findEnclosingClass(other.owner) - } - } - - val cls = findEnclosingClass(c.internal.enclosingOwner) - - assert(cls.isModule || cls.isClass, "Enclosing class is always either a module or a class") - - def loggerByParam(param: c.Tree) = { - q"_root_.zhttp.logging.Logger.getLogger(...${List(param)})" - } - - def loggerBySymbolName(s: Symbol) = { - def fullName(s: Symbol): String = { - @inline def isPackageObject = ( - (s.isModule || s.isModuleClass) - && s.owner.isPackage - && s.name.decodedName.toString == termNames.PACKAGE.decodedName.toString - ) - if (s.isModule || s.isClass) { - if (isPackageObject) { - s.owner.fullName - } else if (s.owner.isStatic) { - s.fullName - } else { - fullName(s.owner) + "." + s.name.encodedName.toString - } - } else { - fullName(s.owner) - } - } - loggerByParam(q"${fullName(s)}") - } - - def loggerByType(s: Symbol) = { - val typeSymbol: ClassSymbol = (if (s.isModule) s.asModule.moduleClass else s).asClass - val typeParams = typeSymbol.typeParams - - if (typeParams.isEmpty) { - loggerByParam(q"_root_.scala.Predef.classOf[$typeSymbol]") - } else { - if (typeParams.exists(_.asType.typeParams.nonEmpty)) { - /* We have at least one higher-kinded type: fall back to by-name logger construction, as - * there's no simple way to declare a higher-kinded type with an "any" parameter. */ - loggerBySymbolName(s) - } else { - val typeArgs = List.fill(typeParams.length)(WildcardType) - val typeConstructor = tq"$typeSymbol[..${typeArgs}]" - loggerByParam(q"_root_.scala.Predef.classOf[$typeConstructor]") - } - } - } - - @inline def isInnerClass(s: Symbol) = { - s.isClass && !(s.owner.isPackage) - } - - val instanceByName = (cls.isModule || cls.isModuleClass) || cls.isClass && isInnerClass(cls) - - if (instanceByName) { - loggerBySymbolName(cls) - } else { - loggerByType(cls) - } - - } - /** A macro context that represents a method call on a Logger instance. */ private[this] type LogCtx = whitebox.Context { type PrefixType = Logger } diff --git a/zio-http-logging/src/main/scala-3/zhttp/logging/Logger.scala b/zio-http-logging/src/main/scala-3/zhttp/logging/Logger.scala index 38863fb9af..853680546c 100644 --- a/zio-http-logging/src/main/scala-3/zhttp/logging/Logger.scala +++ b/zio-http-logging/src/main/scala-3/zhttp/logging/Logger.scala @@ -44,7 +44,6 @@ final class Logger(configuration: Configuration) { object Logger { import scala.language.experimental.macros - inline def getLogger: Logger = ${LoggerMacro.getLoggerImpl} final def getLogger(logLevel: LogLevel) = new Logger(configuration = Configuration(getClass.getSimpleName, logLevel, LogFormat.default)) final def getLogger(loggerName: String) = diff --git a/zio-http-logging/src/main/scala-3/zhttp/logging/macros/LoggerMacro.scala b/zio-http-logging/src/main/scala-3/zhttp/logging/macros/LoggerMacro.scala index e7c2c828a8..d0e87ec6a7 100644 --- a/zio-http-logging/src/main/scala-3/zhttp/logging/macros/LoggerMacro.scala +++ b/zio-http-logging/src/main/scala-3/zhttp/logging/macros/LoggerMacro.scala @@ -12,55 +12,6 @@ import scala.quoted._ */ private[zhttp] object LoggerMacro { - /** Get a logger by reflecting the enclosing class name. */ - final def getLoggerImpl(using qctx: Quotes): Expr[Logger] = { - import qctx.reflect._ - - @tailrec def findEnclosingClass(sym: Symbol): Symbol = { - sym match { - case s if s.isNoSymbol => - report.errorAndAbort("Couldn't find an enclosing class or module for the logger") - case s if s.isClassDef => - s - case other => - /* We're not in a module or a class, so we're probably inside a member definition. Recurse upward. */ - findEnclosingClass(other.owner) - } - } - - def logger(s: Symbol): Expr[Logger] = { - def fullName(s: Symbol): String = { - val flags = s.flags - if (flags.is(Flags.Package)) { - s.fullName - } - else if (s.isClassDef) { - if (flags.is(Flags.Module)) { - if (s.name == "package$") { - fullName(s.owner) - } - else { - val chomped = s.name.stripSuffix("$") - fullName(s.owner) + "." + chomped - } - } - else { - fullName(s.owner) + "." + s.name - } - } - else { - fullName(s.owner) - } - } - - val name = Expr(fullName(s)) - '{ Logger.getLogger($name) } - } - - val cls = findEnclosingClass(Symbol.spliceOwner) - logger(cls) - } - def traceTM(logger: Expr[Logger])(t: Expr[Throwable])(msg: Expr[String])(using qctx: Quotes) = '{ if ($logger.isTraceEnabled) $logger.logger.trace($msg, $t) } def traceM(logger: Expr[Logger])(msg: Expr[String])(using qctx: Quotes) = diff --git a/zio-http/src/main/scala/zhttp/service/Handler.scala b/zio-http/src/main/scala/zhttp/service/Handler.scala index 77a654d996..ccc1f84dc9 100644 --- a/zio-http/src/main/scala/zhttp/service/Handler.scala +++ b/zio-http/src/main/scala/zhttp/service/Handler.scala @@ -181,12 +181,10 @@ private[zhttp] final case class Handler[R]( /** * Executes program */ - private def unsafeRunZIO(program: ZIO[R, Throwable, Any])(implicit ctx: Ctx): Unit = { - log.info(s"Unsafe run $program") + private def unsafeRunZIO(program: ZIO[R, Throwable, Any])(implicit ctx: Ctx): Unit = rt.unsafeRun(ctx) { program } - } override def serverTime: ServerTime = serverTimeGenerator