<@U07LTQFT2> <@U07PEDDGC> my thinking is this in ...
# general
f
@erichelgeson @james.kleeh my thinking is this in java, it is bad practice to do this:
Copy code
String 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
Copy code
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.
Copy code
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
Copy code
<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
Copy code
<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? 🙂
👍 1