Skip to content

Fix macro when interpolated argument is Throwable #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private[scalalogging] object LoggerMacro {
private def formatArgs(c: LoggerContext)(args: c.Expr[Any]*) = {
import c.universe._
args.map { arg =>
c.Expr[AnyRef](if (arg.tree.tpe <:< weakTypeOf[AnyRef]) arg.tree else q"$arg.asInstanceOf[_root_.scala.AnyRef]")
c.Expr[AnyRef](if (arg.tree.tpe <:< weakTypeOf[AnyRef]) q"$arg: _root_.scala.AnyRef" else q"$arg.asInstanceOf[_root_.scala.AnyRef]")
}
}
}
9 changes: 9 additions & 0 deletions src/test/scala/com/typesafe/scalalogging/LoggerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,13 @@ class LoggerSpec extends AnyWordSpec with Matchers with Varargs with MockitoSuga
logger.error(s"""foo\nbar $arg1""")
verify(underlying).error(s"""foo\nbar {}""", arg1)
}

"call the underlying format method when interpolated argument is a Throwable" in {
val f = fixture(_.isErrorEnabled, isEnabled = true)
import f._
logger.error(s"""foo\nbar $arg7""")
verify(underlying).error(s"""foo\nbar {}""", arg7ref)
}
}

"Logging a message using slf4 interpolator and Any args" should {
Expand Down Expand Up @@ -587,6 +594,8 @@ class LoggerSpec extends AnyWordSpec with Matchers with Varargs with MockitoSuga
val arg5ref = arg5.asInstanceOf[AnyRef]
val arg6 = 6L
val arg6ref = arg6.asInstanceOf[AnyRef]
val arg7 = new Throwable
val arg7ref = arg7.asInstanceOf[AnyRef]
val underlying = mock[org.slf4j.Logger]
when(p(underlying)).thenReturn(isEnabled)
val logger = Logger(underlying)
Expand Down