https://kotlinlang.org logo
Title
s

sreich

06/24/2022, 1:45 PM
Is there a way to prevent developers from using kotlin-logging's logger.log("this $expensiveOperation()"), in favor of them doing logger.log { "this $expensiveOperation()" } ? https://github.com/MicroUtils/kotlin-logging
c

Chris Lee

06/24/2022, 1:46 PM
perhaps a detekt rule that flags this?
j

Joffrey

06/24/2022, 1:46 PM
Do you mean a specific
expensiveOperation
? Or any method call? You could write a wrapper for
logger.log
that provides a scope to call the operation only in the lambda form, but that would be a bit convoluted
s

sreich

06/24/2022, 1:48 PM
aynthing, just so that they are using the lambda form always
j

Joffrey

06/24/2022, 1:48 PM
ah
s

sreich

06/24/2022, 1:48 PM
since the lambda form is the only one that can be optimized out
j

Joffrey

06/24/2022, 1:49 PM
maybe you could go with a custom IDEA inspection that flags all usages of
logger.log(String)
- but that would only work on this IDE. Otherwise yeah a rule on whatever linter you're using would be best
s

sreich

06/24/2022, 2:08 PM
hmmm that would work.
are those trivial to add? can you make it auto replace them?
oooh these are nice. i'll read up on them, very powerful
j

Joffrey

06/24/2022, 2:37 PM
Sorry for the delay, I had missed your messages. I'm not sure which solution you're talking about, is it IDEA inspections? Or Detekt? I'm not very well versed in either to be honest, but I know IDEA inspections are pretty nice, especially if you share
.idea
and other devs use IDEA too
s

sreich

06/24/2022, 2:40 PM
just intellij inspections. yeah precisely, looks like i can do a lot with them. i'll take a look. thanks
👍 1