File tree Expand file tree Collapse file tree 2 files changed +14
-4
lines changed
main/scala/com/typesafe/scalalogging
test/scala/com/typesafe/scalalogging Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -239,8 +239,13 @@ private object LoggerMacro {
239239
240240 message.tree match {
241241 case q " scala.StringContext.apply(.. $parts).s(.. $args) " =>
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(" {}" )
242+ val messageFormat = parts.iterator.map({ case Literal (Constant (str : String )) => str })
243+ // Emulate standard interpolator escaping
244+ .map(StringContext .treatEscapes)
245+ // Escape literal slf4j format anchors if the resulting call will require a format string
246+ .map(str => if (args.nonEmpty) str.replace(" {}" , " \\ {}" ) else str)
247+ .mkString(" {}" )
248+
244249 (c.Expr (q " $messageFormat" ), args map { arg =>
245250 // Box interpolated AnyVals by explicitly getting the string representation
246251 c.Expr [Any ](if (arg.tpe <:< weakTypeOf[AnyVal ]) q " $arg.toString " else arg)
Original file line number Diff line number Diff line change @@ -68,13 +68,18 @@ class LoggerSpec extends WordSpec with Matchers with MockitoSugar {
6868 verify(underlying).error(" msg {}" , 1 .toString)
6969 }
7070
71- " call the underlying logger's error method preserving literal format anchors when the message is interpolated " in {
71+ " call the underlying logger's error method escaping literal format anchors" in {
7272 val f = fixture(_.isErrorEnabled, true )
7373 import f ._
7474 logger.error(s " foo {} bar $arg1" )
7575 verify(underlying).error(" foo \\ {} bar {}" , arg1)
7676 }
77-
77+ " call the underlying logger's error method without escaping format anchors when the message has no interpolations" in {
78+ val f = fixture(_.isErrorEnabled, true )
79+ import f ._
80+ logger.error(s " foo {} bar " )
81+ verify(underlying).error(" foo {} bar" )
82+ }
7883 " call the underlying logger's error method when the interpolated string contains escape sequences" in {
7984 val f = fixture(_.isErrorEnabled, true )
8085 import f ._
You can’t perform that action at this time.
0 commit comments