Logger Resolvers

The easiest way to get at a logger is to use LoggerFactory. This looks like the SLF4J version, but is more flexible because it uses a LoggerResolver type class under the hood.

sourceimport com.tersesystems.blindsight.LoggerFactory

val loggerFromName  = LoggerFactory.getLogger("some.Logger")
val loggerFromClass = LoggerFactory.getLogger(getClass)

There is also a macro based version which finds the enclosing class name and hands it to you:

sourceval loggerFromEnclosing = LoggerFactory.getLogger

Finally, you also have the option of creating your own LoggerResolver. This is useful when you want to get away from class based logging, and use a naming strategy based on a correlation id.

For example, you can resolve a logger using a request:

sourceimplicit val requestToResolver: LoggerResolver[Request] = new LoggerResolver[Request] {
  override def resolveLogger(instance: Request): org.slf4j.Logger = {
    org.slf4j.LoggerFactory.getLogger("requests." + instance.id)
  }
}

And from then on, you can do:

sourcedef loggerFromRequest(request: Request): Unit = {
  val logger = com.tersesystems.blindsight.LoggerFactory.getLogger(request)
}
The source code for this page can be found here.