What is the difference between scala StrictLogging and Lazylogging?

codingsplash picture codingsplash · Jan 14, 2017 · Viewed 9.5k times · Source

Consider the below code which uses scala logging:

class MyClass extends LazyLogging {
  logger.debug("This is very convenient")
}

What is the difference if I use StrictLogging instead? When should I use which?

Edit: I know what lazy is. But I am unable to get it from logging perspective and how does it differ from its functionality when compared to strict logging. This is so that I can understand when to use which one.

Answer

Cole Stanfield picture Cole Stanfield · Mar 16, 2017

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?