@@ -21,14 +21,15 @@ import scala.reflect.macros.blackbox.Context
2121
2222private object LoggerMacro {
2323
24+ private final val ArgumentMarker = " {}"
25+
2426 type LoggerContext = Context { type PrefixType = Logger }
2527
2628 // Error
2729
2830 def errorMessage (c : LoggerContext )(message : c.Expr [String ]) = {
29- import c .universe ._
30- val underlying = q " ${c.prefix}.underlying "
31- q " if ( $underlying.isErrorEnabled) $underlying.error( $message) "
31+ val (messageFormat, args) = deconstructInterpolatedMessage(c)(message)
32+ errorMessageArgs(c)(messageFormat, args : _* )
3233 }
3334
3435 def errorMessageCause (c : LoggerContext )(message : c.Expr [String ], cause : c.Expr [Throwable ]) = {
@@ -47,9 +48,8 @@ private object LoggerMacro {
4748 }
4849
4950 def errorMessageMarker (c : LoggerContext )(marker : c.Expr [Marker ], message : c.Expr [String ]) = {
50- import c .universe ._
51- val underlying = q " ${c.prefix}.underlying "
52- q " if ( $underlying.isErrorEnabled) $underlying.error( $marker, $message) "
51+ val (messageFormat, args) = deconstructInterpolatedMessage(c)(message)
52+ errorMessageArgsMarker(c)(marker, messageFormat, args : _* )
5353 }
5454
5555 def errorMessageCauseMarker (c : LoggerContext )(marker : c.Expr [Marker ], message : c.Expr [String ], cause : c.Expr [Throwable ]) = {
@@ -70,9 +70,8 @@ private object LoggerMacro {
7070 // Warn
7171
7272 def warnMessage (c : LoggerContext )(message : c.Expr [String ]) = {
73- import c .universe ._
74- val underlying = q " ${c.prefix}.underlying "
75- q " if ( $underlying.isWarnEnabled) $underlying.warn( $message) "
73+ val (messageFormat, args) = deconstructInterpolatedMessage(c)(message)
74+ warnMessageArgs(c)(messageFormat, args : _* )
7675 }
7776
7877 def warnMessageCause (c : LoggerContext )(message : c.Expr [String ], cause : c.Expr [Throwable ]) = {
@@ -91,9 +90,8 @@ private object LoggerMacro {
9190 }
9291
9392 def warnMessageMarker (c : LoggerContext )(marker : c.Expr [Marker ], message : c.Expr [String ]) = {
94- import c .universe ._
95- val underlying = q " ${c.prefix}.underlying "
96- q " if ( $underlying.isWarnEnabled) $underlying.warn( $marker, $message) "
93+ val (messageFormat, args) = deconstructInterpolatedMessage(c)(message)
94+ warnMessageArgsMarker(c)(marker, messageFormat, args : _* )
9795 }
9896
9997 def warnMessageCauseMarker (c : LoggerContext )(marker : c.Expr [Marker ], message : c.Expr [String ], cause : c.Expr [Throwable ]) = {
@@ -114,9 +112,8 @@ private object LoggerMacro {
114112 // Info
115113
116114 def infoMessage (c : LoggerContext )(message : c.Expr [String ]) = {
117- import c .universe ._
118- val underlying = q " ${c.prefix}.underlying "
119- q " if ( $underlying.isInfoEnabled) $underlying.info( $message) "
115+ val (messageFormat, args) = deconstructInterpolatedMessage(c)(message)
116+ infoMessageArgs(c)(messageFormat, args : _* )
120117 }
121118
122119 def infoMessageCause (c : LoggerContext )(message : c.Expr [String ], cause : c.Expr [Throwable ]) = {
@@ -135,9 +132,8 @@ private object LoggerMacro {
135132 }
136133
137134 def infoMessageMarker (c : LoggerContext )(marker : c.Expr [Marker ], message : c.Expr [String ]) = {
138- import c .universe ._
139- val underlying = q " ${c.prefix}.underlying "
140- q " if ( $underlying.isInfoEnabled) $underlying.info( $marker, $message) "
135+ val (messageFormat, args) = deconstructInterpolatedMessage(c)(message)
136+ infoMessageArgsMarker(c)(marker, messageFormat, args : _* )
141137 }
142138
143139 def infoMessageCauseMarker (c : LoggerContext )(marker : c.Expr [Marker ], message : c.Expr [String ], cause : c.Expr [Throwable ]) = {
@@ -158,9 +154,8 @@ private object LoggerMacro {
158154 // Debug
159155
160156 def debugMessage (c : LoggerContext )(message : c.Expr [String ]) = {
161- import c .universe ._
162- val underlying = q " ${c.prefix}.underlying "
163- q " if ( $underlying.isDebugEnabled) $underlying.debug( $message) "
157+ val (messageFormat, args) = deconstructInterpolatedMessage(c)(message)
158+ debugMessageArgs(c)(messageFormat, args : _* )
164159 }
165160
166161 def debugMessageCause (c : LoggerContext )(message : c.Expr [String ], cause : c.Expr [Throwable ]) = {
@@ -179,9 +174,8 @@ private object LoggerMacro {
179174 }
180175
181176 def debugMessageMarker (c : LoggerContext )(marker : c.Expr [Marker ], message : c.Expr [String ]) = {
182- import c .universe ._
183- val underlying = q " ${c.prefix}.underlying "
184- q " if ( $underlying.isDebugEnabled) $underlying.debug( $marker, $message) "
177+ val (messageFormat, args) = deconstructInterpolatedMessage(c)(message)
178+ debugMessageArgsMarker(c)(marker, messageFormat, args : _* )
185179 }
186180
187181 def debugMessageCauseMarker (c : LoggerContext )(marker : c.Expr [Marker ], message : c.Expr [String ], cause : c.Expr [Throwable ]) = {
@@ -202,9 +196,8 @@ private object LoggerMacro {
202196 // Trace
203197
204198 def traceMessage (c : LoggerContext )(message : c.Expr [String ]) = {
205- import c .universe ._
206- val underlying = q " ${c.prefix}.underlying "
207- q " if ( $underlying.isTraceEnabled) $underlying.trace( $message) "
199+ val (messageFormat, args) = deconstructInterpolatedMessage(c)(message)
200+ traceMessageArgs(c)(messageFormat, args : _* )
208201 }
209202
210203 def traceMessageCause (c : LoggerContext )(message : c.Expr [String ], cause : c.Expr [Throwable ]) = {
@@ -223,9 +216,8 @@ private object LoggerMacro {
223216 }
224217
225218 def traceMessageMarker (c : LoggerContext )(marker : c.Expr [Marker ], message : c.Expr [String ]) = {
226- import c .universe ._
227- val underlying = q " ${c.prefix}.underlying "
228- q " if ( $underlying.isTraceEnabled) $underlying.trace( $marker, $message) "
219+ val (messageFormat, args) = deconstructInterpolatedMessage(c)(message)
220+ traceMessageArgsMarker(c)(marker, messageFormat, args : _* )
229221 }
230222
231223 def traceMessageCauseMarker (c : LoggerContext )(marker : c.Expr [Marker ], message : c.Expr [String ], cause : c.Expr [Throwable ]) = {
@@ -243,4 +235,18 @@ private object LoggerMacro {
243235 q " if ( $underlying.isTraceEnabled) $underlying.trace( $marker, $message, .. $args) "
244236 }
245237
238+ /** Checks whether `messsage` is an interpolated string and transforms it into SLF4J string interpolation. */
239+ private def deconstructInterpolatedMessage (c : LoggerContext )(message : c.Expr [String ]) = {
240+ import c .universe ._
241+
242+ message.tree match {
243+ case q " scala.StringContext.apply(.. $parts).s(.. $args) " =>
244+ val strings = parts.collect { case Literal (Constant (s : String )) => s }
245+ val messageFormat = strings.mkString(ArgumentMarker )
246+ (c.Expr (q " $messageFormat" ), args.map(arg => q " $arg" ).map(c.Expr [Any ](_)))
247+
248+ case _ => (message, Seq .empty)
249+ }
250+ }
251+
246252}
0 commit comments