Skip to content

Commit 0f59f97

Browse files
committed
Box interpolated values as necessary
Box interpolated values for consistency in testing and to avoid runtime checks for expressions of type Any.
1 parent cf35016 commit 0f59f97

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/main/scala/com/typesafe/scalalogging/LoggerMacro.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,18 @@ private object LoggerMacro {
239239

240240
message.tree match {
241241
case q"scala.StringContext.apply(..$parts).s(..$args)" =>
242-
val messageFormat = parts.iterator.map({ case Literal(Constant(str: String)) => str })
242+
val format = parts.iterator.map({ case Literal(Constant(str: String)) => str })
243243
// Emulate standard interpolator escaping
244244
.map(StringContext.treatEscapes)
245245
// Escape literal slf4j format anchors if the resulting call will require a format string
246246
.map(str => if (args.nonEmpty) str.replace("{}", "\\{}") else str)
247247
.mkString("{}")
248248

249-
(c.Expr(q"$messageFormat"), args map { arg =>
250-
// Box interpolated AnyVals by explicitly getting the string representation
251-
c.Expr[Any](if (arg.tpe <:< weakTypeOf[AnyVal]) q"$arg.toString" else arg)
252-
})
249+
val formatArgs = args map { arg =>
250+
c.Expr[AnyRef](if (arg.tpe <:< weakTypeOf[AnyRef]) arg else q"$arg.asInstanceOf[_root_.scala.AnyRef]")
251+
}
252+
253+
(c.Expr(q"$format"), formatArgs)
253254

254255
case _ => (message, Seq.empty)
255256
}

src/test/scala/com/typesafe/scalalogging/LoggerSpec.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,18 @@ class LoggerSpec extends WordSpec with Matchers with MockitoSugar {
420420

421421
"Logging a message using the standard string interpolator" should {
422422

423-
"call the underlying format method with the string representation of AnyVal arguments" in {
423+
"call the underlying format method with boxed versions of value arguments" in {
424424
val f = fixture(_.isErrorEnabled, true)
425425
import f._
426426
logger.error(s"msg ${1}")
427-
verify(underlying).error("msg {}", 1.toString)
427+
verify(underlying).error("msg {}", 1.asInstanceOf[AnyRef])
428+
}
429+
430+
"call the underlying format method with boxed versions of arguments of type Any" in {
431+
val f = fixture(_.isErrorEnabled, true)
432+
import f._
433+
logger.error(s"msg ${1.asInstanceOf[Any]}")
434+
verify(underlying).error("msg {}", 1.asInstanceOf[AnyRef])
428435
}
429436

430437
"call the underlying format method escaping literal format anchors" in {

0 commit comments

Comments
 (0)