Skip to content

Commit 11623e4

Browse files
committed
Emulate standard interpolator escape treatment
1 parent 0679a2b commit 11623e4

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ private object LoggerMacro {
239239

240240
message.tree match {
241241
case q"scala.StringContext.apply(..$parts).s(..$args)" =>
242-
// Escape slf4j format anchors
243-
val messageFormat = parts.map({ case Literal(Constant(s: String)) => s.replace("{}", "\\{}") }).mkString("{}")
242+
// Emulate standard interpolator escaping and escape literal slf4j format anchors
243+
val messageFormat = parts.map({ case Literal(Constant(s: String)) => StringContext.treatEscapes(s).replace("{}", "\\{}") }).mkString("{}")
244244
(c.Expr(q"$messageFormat"), args map { arg =>
245245
// Box interpolated AnyVals by explicitly getting the string representation
246246
c.Expr[Any](if (arg.tpe <:< weakTypeOf[AnyVal]) q"$arg.toString" else arg)

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ class LoggerSpec extends WordSpec with Matchers with MockitoSugar {
7474
logger.error(s"foo {} bar $arg1")
7575
verify(underlying).error("foo \\{} bar {}", arg1)
7676
}
77+
78+
"call the underlying logger's error method when the interpolated string contains escape sequences" in {
79+
val f = fixture(_.isErrorEnabled, true)
80+
import f._
81+
logger.error(s"foo\nbar $arg1")
82+
verify(underlying).error(s"foo\nbar {}", arg1)
83+
}
84+
"call the underlying logger's error method when the interpolated string is triple quoted and contains escape sequences" in {
85+
val f = fixture(_.isErrorEnabled, true)
86+
import f._
87+
logger.error(s"""foo\nbar $arg1""")
88+
verify(underlying).error(s"""foo\nbar {}""", arg1)
89+
}
7790
}
7891

7992
"Calling error with a message and cause" should {

0 commit comments

Comments
 (0)