What is the difference between scala StrictLogging and Lazylogging?

I'm assuming you're refering to typesafes scala-logging library. The difference is only in the way logger is defined by the underlying logger, it can either be defined as a normal value, or a lazy value, meaning:

trait LazyLogging {
  @volatile protected lazy val logger: Logger =
      Logger(LoggerFactory.getLogger(getClass.getName))
}

vs

trait StrictLogging {
  protected val logger: Logger =
    Logger(LoggerFactory.getLogger(getClass.getName))
}

Well, everyone seems to have covered what lazy means but didn't really mention how this affects your choice so I'll try to list the guidelines I follow myself.

  • Use StrictLogging pretty much by default, especially if the class is a singleton or you know the log methods will always get called.

  • Use LazyLogging if you're creating lots of objects with this trait repetitively. If you're doing that though, you may think about putting this logger in a companion object of the class so you don't keep instantiating Logger and calling LoggerFactory.getLogger(...) over and over.

Can anyone think of other reasons to use/not use LazyLogging over StrictLogging?


What is the difference between scala StrictLogging and Lazylogging?

Let's first read both trait definitions;

trait LazyLogging {
 @volatile protected lazy val logger: Logger =
 Logger(LoggerFactory.getLogger(getClass.getName))
}

trait StrictLogging {
protected val logger: Logger =
 Logger(LoggerFactory.getLogger(getClass.getName))
}

The main difference is @volatile and lazy keywords.

How lazy works?

The main characteristic of a lazy val is that the bound expression is not evaluated immediately, but once on the first access. When the initial access happens, the expression is evaluated. On subsequent access, no further evaluation occurs, instead the stored result is returned immediately.

For multithread scenario, @volatile will force memory into consistent state across multiple threads.

Actually, it depends on the individual use case which trait to use LazyLogging or StrictLogging.