Hi Tim,
the MDC thingy is—to my mind—just an ugly hack to pass arguments “around” the real interface, presumably because someone thought that doing it properly would look too cumbersome in Java (just guessing).
I completely understand the desire to add more data to log statements. The most obvious choice would be to include those data in the log message. If some more structure is needed, why not pass a Map[String, AnyRef] along with the log event? Then you could use the MDC for the SLF4J “back-end façade” or whatever else in some other case (is anybody still logging directly into MySQL?). As Alec said, Logging.scala is a rather simple thing, so the only thing missing (taking an educated guess here) is that you’d need to pass along your auxiliary data through the call stack. Using Scala I’d recommend trying out using implicit arguments of type
case class MoreLogData(val dict: Map[String, AnyRef])
object MoreLogData {
def add(key: String, value: AnyRef)(implicit mld: MoreLogData) = mld.copy(mld.dict + (key -> value))
def remove(key: String)(implicit mld: MoreLogData) = mld.copy(mld.dict - key)
}
in your code. I’d call that a “more principled replacement for thread-locals” ;-)
def myMethod(...)(implicit mld: MoreLogData) = {
...
myOtherMethod(...) // implicitly picks up mld
...
yetAnotherMethod(...)(MoreLogData add "hello" -> "world") // explicit addition
}
If you try this and find it to work well, please let us know (possibly in the form of a pull request?).
Regards,
Roland