I have an spring boot project with default log configuration using logback. I have always used this approach for logging purpose inside my applications:
logger.info("Get user paginated: filter {}",user)
Now i am learning Kotlin and I was reading about String templates.If i rewrite my sentence using $ would be:
logger.info("Get user paginated: filter $user")
Which would be the better way to log in Kotlin with Spring boot?
The better way is neither; add https://github.com/MicroUtils/kotlin-logging and write
logger.info { "Get user paginated: filter $user" }
with the advantages of both.
The point of the {}
syntax supported by SLF4J is to avoid useless String concatenation and toString() calls if the log message is never actually generated (for example because the level is higher than info.
A template string doesn't avoid that: the user.toString()
method needs to be called, and its result needs to be appended to the static part of the log message, before being passed to logger.info()
.
So, if you're looking for the best performance, use SL4J parameters. If you don't care and find the template string more readable, use the template string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With