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.
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.
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
?
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)) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With