Earlier I normally used to use log.info(“dsasdds ” + var)
to add logs but now, in my new workplace, I see everyone uses log.info(“dsasdds {}”, var)
way to print logs? Is there any significant difference between these 2 ways on how they work internally?
Asked
Active
Viewed 337 times
0

hatellla
- 4,796
- 8
- 49
- 101
-
The former uses string concatenation, which is easy to use. The later uses parameters, which is easier to edit and format, especially in complex logging statements. There's also performance-related matters. – Compass Jun 13 '19 at 19:52
1 Answers
2
This depends on the computation of the variable printed in the log.
Supposing that var.toString()
is relatively expensive and that the info
level is not enabled for the current logger, by using log.info(“dsasdds ” + var)
a relatively expensive operation was computed for "nothing" since no logs were written.
With log.info(“dsasdds {}”, var)
, the var.toString()
will be invoked only if level of the current logger match with the level requested (INFO
here).
For expressions, suppliers (that are lazily evaluated) is also a possibility :
logger.trace("Some long-running operation returned {}", () -> expensiveOperation());
Now is a good practice to use systematically lazy evaluated expressions/variables ? Not necessarily but that doesn't hurt either in terms of readability in your example.

davidxxx
- 125,838
- 23
- 214
- 215