https://kotlinlang.org logo
e

Emmanuel Oga

09/26/2020, 7:29 PM
is there any logging solution for Kotlin able to perform a NOP through configuration? I'm currently using SLF4J, with calls like
<http://logger.info|logger.info>("${PERFORM POTENTIALLY EXPENSIVE LOGGING OP}")
, so even if I configure the logger to only log errors, the info call will still incur the cost of the expensive operation, with no message being logged. Would be nice to have it so the logging doesn't even happen at all unless I turn it on.
I wonder how it works...
n

nanodeath

09/26/2020, 7:33 PM
you can also say either
if (logger.isInfoEnabled()) <http://logger.info|logger.info>(...)
or
<http://logger.info|logger.info>(...) {}
, but neither are ideal compared to more Kotlin-y solutions like what you found
e

Emmanuel Oga

09/26/2020, 7:34 PM
ah, I think that solution just takes the lambda and doesn't call it if the logging level doesn't match
k

kenkyee

09/26/2020, 7:34 PM
Log4j2 also has a way to pass methods IIRC. They don't get called unless that logging level is enabled...
n

nanodeath

09/26/2020, 7:34 PM
I believe there are some libraries that completely compile out if the logging level is configured appropriately, but they're also less configurable
e

Emmanuel Oga

09/26/2020, 7:34 PM
right, I guess I was over thinking it, I was thinking of not even outputting the lambda on the build, but that seems extreme, probably not gonna gain a lot from that
I heard proguard can be used for that kind of thing
☝️ 1
n

Nir

09/26/2020, 8:43 PM
@Emmanuel Oga you asked about return in functions vs not in lambdas before right?
This is a great example of what ultra concise lambdas give you
Basically, lazy parameters :-)
e

Emmanuel Oga

09/26/2020, 8:56 PM
good point
n

nfrankel

09/26/2020, 9:23 PM
i played with the idea a while ago https://blog.frankel.ch/smart-logging-in-java8-kotlin/ it seems log4j2 does the same natively
👍 1
r

Reed Ellsworth

02/21/2021, 4:55 PM
Using a lambda syntax does make it so it is not executed when logging is disabled. That is one of the reasons that drove me to writing KmLogging was that I wanted very little overhead when the product is in the field. I needed one that was multiplatform which microutils is not.