Many logging frameworks (e.g. log4j) allow you to pass lambda expressions instead of String to the logging APIs. The argument is that if a string is particularly expressive for construction, the string construction can be lazily performed using the lambda expression. Thus, a line is created only if the syslog level matches the call level.
But, given that modern compilers make many methods, inserting them automatically, does it really make sense to use lambda expressions in this way? I will give a simplified example below to demonstrate this problem.
Suppose our traditional logging method is as follows:
void log(int level, String message) { if (level >= System.logLevel) System.out.println(message); }
Assume that FINE less than CRITICAL , therefore, although an expensive line is built, all this is not so, since the message is not displayed.
Lambda logging APIs help in this situation, so the string is only evaluated (constructed) if necessary:
void log(int level, Supplier<String> message) { if (level >= System.logLevel) System.out.println(message.apply()); }
But is it possible that the compiler can simply embed the logging method so that the network effect is as follows:
System.logLevel = Level.CRITICAL; if (Level.FINE >= System.logLevel) System.out.println("Very expensive string to construct..." + etc);
In this case, we do not need to evaluate the string before calling the registration API (because it does not exist), and, presumably, we would get performance only from the attachment.
In conclusion, my question is, how do lambda expressions help us in this situation, given that the compiler can embed logging API calls? The only thing I can think of is that somehow, in the case of lambda, the string is not calculated if the logging level does not match.