Skip to content

Commit 44244a4

Browse files
committed
Update documentation
Add test with 2 interpolated arguments
1 parent 7f1eaac commit 44244a4

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if (logger.isDebugEnabled) logger.debug(s"Some $expensive message!")
2323
A compatible logging backend is [Logback](http://logback.qos.ch), add it to your sbt build definition:
2424

2525
```scala
26-
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.7"
26+
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"
2727
```
2828

2929
If you are looking for a version compatible with Scala 2.10, check out Scala Logging 2.x.
@@ -94,6 +94,28 @@ class MyClass extends LazyLogging {
9494
- SLF4J loggers and our Logger now survive serialization. By survive serialization, we mean that the
9595
deserialized logger instances are fully functional.
9696

97+
## String Interpolation
98+
It is idiomatic to use Scala's string interpolation `logger.error(s"log $value")` instead of SLF4J string interpolation `logger.error("log {}", value)`.
99+
However there are some tools (such as [Sentry](https://sentry.io)) that use the log message format as grouping key. Therefore they do not work well with
100+
Scala's string interpolation.
101+
102+
Scala Logging replaces simple string interpolations with their SLF4J counterparts like this:
103+
104+
```scala
105+
logger.error(s"my log message: $arg1 $arg2 $arg3")
106+
```
107+
108+
```scala
109+
logger.error("my log message: {} {} {}", arg1, arg2, arg3)
110+
```
111+
112+
This has no effect on behavior and performace should be comparable (depends on the underlying logging library).
113+
114+
### Limitations
115+
- Works only when string interpolation is directly used inside the logging statement. That is when the log message is static (available at compile time).
116+
- Works only for the `logger.<level>(message)` and `logger.<level>(marker, message)` logging methods. It does not work if you want to log an exception and
117+
use string interpolation too (this is a limitation of the SLF4J API).
118+
97119
## Line numbers in log message?
98120

99121
Using the [sourcecode](https://github.com/lihaoyi/sourcecode#logging) library, it's possible to add line number

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ class LoggerSpec extends WordSpec with Matchers with MockitoSugar {
5050
logger.error(s"msg $arg1 $arg2 $arg3")
5151
verify(underlying).error("msg {} {} {}", arg1, arg2, arg3)
5252
}
53+
54+
"call the underlying logger's error method with two arguments if the error level is enabled and string is interpolated" in {
55+
val f = fixture(_.isErrorEnabled, true)
56+
import f._
57+
logger.error(s"msg $arg1 $arg2")
58+
verify(underlying).error("msg {} {}", List(arg1, arg2): _*)
59+
}
5360
}
5461

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

0 commit comments

Comments
 (0)