Log methods without checking log level


#1

I am looking for log library which support “log methods without checking whether the respective log level is enabled”.
Smething like https://github.com/typesafehub/scala-logging.

Is it even possible?
Thanks.


#2

With Kotlin, you can create wrappers for your favorite logging library with syntax like:

log.debug { "log statement goes here" }

I’m not sure if there are existing wrappers for any library that support such syntax, but if not, it’s fairly trivial to add them as part of your own project. Something like:

 fun Logger.debug(callback: () -> String) {
    if (isDebugEnabled) debug(callback())
 }

#3

Here’s my wrapper functions for SLF4J: https://gist.github.com/vbezhenar/d31820773b76de57472febbdbeb690f6


#4

java.util.logging added support for this with java 8, there is an overload to take a Supplier<String> for all of the named methods, that + kotlins SAM conversions means, exactly as yole pointed out:

log.warning { "$importantThing is not alpha-beta-able!!" }

is runnable standard java-8 kotlin code.

That said, I doubt JUL is anybody’s favourite logging framework, if there is such a thing. I have only ever been annoyed by logging.

And also worth mentioning, If you’re logging screamingly hot loops, then kotlins inline strategy for lambdas might make an extension method faster, because JUL’s supplier will use the lambda meta factory whereas an inlined kotlin lambda will simply be a code block in an if branch.