Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should I be using slf4j isTraceEnabled or not?

Tags:

java

scala

slf4j

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?

like image 998
Jas Avatar asked Sep 13 '25 22:09

Jas


2 Answers

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.

like image 146
Henry Avatar answered Sep 15 '25 12:09

Henry


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).

like image 26
Alexey Romanov Avatar answered Sep 15 '25 12:09

Alexey Romanov