Closed EotT123 closed 5 months ago
A String template evaluates eagerly. There is no other way since there is a single expression that must evaluate to a String value.
Of course, you can add your own laziness.
String pet = "dog";
logger.debug("{}", message(()-> "my $pet has fleas"));
. . .
Object message(Supplier<String> msg) {
return new Object() {
public String toString() {
return msg.get();
}
};
}
Would be a good idea to add nicer extension methods to your logger class if you did go this route, so you could write:
logger.debug(()-> "my $pet has fleas");
Thanks for the suggestion!
Are String Templates evaluated eagerly or lazily? When using logging statements with traditional replacements, lazy evaluation is beneficial because the evaluation depends on the logging level.