Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LazyLogging is debug enabled?

Tags:

logging

scala

I came from Java world and in Java we used to test if debug is enabled before logging with level debug. Something like this:

if(logger.isDebugEnabled)
    logger.debug("Debug")

This is in order to avoid evaluation of arguments passed into logger.debug. Do we have to do the same with scala LazyLogging trait?

class Test extends LazyLogging{
   val veryBigSeq: Seq[String] = //...
   logger.debug(veryBigSeq.toString())  //<<----- Here
}
like image 355
Some Name Avatar asked Jun 17 '26 21:06

Some Name


1 Answers

As you can see, the code

if(logger.isDebugEnabled)
    logger.debug("Debug")

is pretty repetitive, and can easily be generated automatically just from the part of the AST that is passed as an argument to logger.debug(...). It would be surprising if this wasn't implemented as a macro. Indeed, if you take a look at the actual code in Logger.scala, then you will see that all those .debug and .error methods are actually macros. Therefore, all the boilerplate will be added automatically, no if-s around each .debug are required.

like image 109
Andrey Tyukin Avatar answered Jun 20 '26 01:06

Andrey Tyukin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!