I’ve been reading Logback documentation today and found an interesting moment about its speed of logging.
Assuming we have set a logging level higher than DEBUG (INFO, WARN or ERROR) and then we try to log something like:
Object someObject = new Object(); Logger log = LoggerFactory.getLogger("com.foo"); log.debug("Message about" + String.valueOf(object));
This approach takes long time to calculate because independent of the fact that log statement may be disabled that calcualtion of String value of the object will take place.
To avoid that we can write something like:
Object someObject = new Object(); Logger log = LoggerFactory.getLogger("com.foo"); log.debug("Message about {}", object);
This time the conversion will take place only if the log statement is expected to be calculated due to logging level, that is – it is disabled.
What is really interesting is that the second way will be 30 times faster!
Source:
https://logback.qos.ch/manual/architecture.html (Better alternative chapter)