Franz See
05/28/2019, 4:03 AMString value = "..."
<http://log.info|log.info>("Hello " + value);
That's because the string manipulation (i.e. in this case, string concatenation) can substantially reduce performance. And the best practice for this one before (i.e. pre-slf4j) was to do the following
String value = "..."
if (log.isInfoEnabled()) {
<http://log.info|log.info>("Hello " + value);
}
This then added a lot of clutter in java codes pre-slf4j days. And then slf4j came and then introduced their placeholders.
String value = "..."
<http://log.info|log.info>("Hello {}", value);
Which meant, "{}" will not be replaced by value not unless log.isInfoEnabled() evaluates to true (Note: log.isInfoEnabled() is invoked inside slf4j's <http://log.info|log.info>())
Now going to groovy, if we have this
<http://log.info|log.info>("Hello ${value}")
Then we go back to the original issue which slf4j tries to prevent in the first place - string manipulation without checking log level.
So to fix this, we can change this to
<http://log.info|log.info>('Hello {}', value)
This then avoids the unnecessary GString manipulation if isInfoEnabled() is false
Did I get that right or is there some sort of special compiler optimisation that automagically handles this? 🙂