Clean Code Rules for Logging
July 16, 2022 ยท View on GitHub
Do not use standard io streams
Rule-ID: logging.no-streams
Do not use classes and methods like System.out.println() or ex.printStacktrace(), but instead
use org.slf4j.Logger. This ensures that all log messages go to the same file and enables you
to have managed logging by selectively turning on and off logging. An example of logging done right
looks like the following:
package io.cloudflight.my_fancy_project.service
import mu.KotlinLogging
@Service
class MyService{
fun myFunction(){
LOG.debug("my debug message")
}
companion object {
private val LOG = KotlinLogging.logger {}
}
}
If you do not see your output on stdout, your active loglevel might be the cause. Adapt your logback-spring.xml in that case.
Do not use Java Util Logging
Rule-ID: logging.no-java-util-logging
Do not use Java Util Logging for your loggers, but instead Slf4J.
Loggers should be private static final
Rule-ID: logging.static-final-loggers
Instances of org.slf4j.Logger should be private, static and final.
Do not expose loggers via public methods
Rule-ID: logging.do-not-expose-loggers-via-methods
Instances of org.slf4j.Logger should not be made accessible via public methods and therefore exposed. They
should stay private within your classes. If you make loggers accessible to other objects, it is really
hard to read and filter logs accordingly.
This violation is also triggered if you have your logger as non-private val in your Kotlin companion object:
class MyService{
fun myFunction(){
LOG.debug("my debug message")
}
companion object {
val LOG = KotlinLogging.logger {}
}
}
Make the logger private here to not make it accessible from outside.
Do not extend from mu.KLogging
Rule-ID: logging.do-not-extend-klogging
Although the documentation of KLogging tells you to write code like this:
class MyClassWithPublicLogger3 {
fun foo() {
logger.info("")
}
companion object : KLogging() {
}
}
We advise not to do that, as mu.KLogging also exposes the logger as public val and therefore again
makes the logger accessible to any other object.