should I be using slf4j isTraceEnabled or not?

2020-07-26 12:27发布

问题:

If I print lot of data in case trace is enabled should i be using isTraceEnabled or just do my log.trace("{} mymessage", "param") would there be any benefit in this case to using isTraceEnabled or no benefit?

回答1:

That depends what "param" actually is. If it is a complex expression it is better to use isTraceEnabled. If it is just a reference to an object you can use it directly. Its toString method will only be called if tracing is enabled.

Using isTraceEnabled is also beneficial if you have several trace statements in a row. You can can put them into the same if then.



回答2:

While Henry's answer is correct, I wanted to add that trace will effectively have to call isTraceEnabled anyway, so there is actually a (very small) drawback to calling isTraceEnabled when it isn't useful (i.e. you don't need to construct the parameters by non-trivial expressions and you only have a single trace call).



标签: java scala slf4j