is there any logging solution for Kotlin able to p...
# announcements
e
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
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
ah, I think that solution just takes the lambda and doesn't call it if the logging level doesn't match
k
Log4j2 also has a way to pass methods IIRC. They don't get called unless that logging level is enabled...
n
I believe there are some libraries that completely compile out if the logging level is configured appropriately, but they're also less configurable
e
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
@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
good point
n
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
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.