Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between scala StrictLogging and Lazylogging?

Tags:

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.

like image 956
codingsplash Avatar asked Jan 14 '17 10:01

codingsplash


People also ask

What is LazyLogging in Scala?

trait LazyLogging extends AnyRefDefines logger as a lazy value initialized with an underlying org. slf4j. Logger named according to the class into which this trait is mixed.


2 Answers

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?

like image 76
Cole Stanfield Avatar answered Sep 22 '22 09:09

Cole Stanfield


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)) } 
like image 41
Yuval Itzchakov Avatar answered Sep 22 '22 09:09

Yuval Itzchakov