Should I use slf4j isTraceEnabled or not?

If I print a lot of data in case tracing is enabled, I have to use isTraceEnabled or just do my log.trace("{} mymessage", "param") was there any benefit from using isTraceEnabled in this case or not?

+1
source share
2 answers

It depends on what "param" actually is. If this is a complex expression, it is better to use isTraceEnabled . If this is just an object reference, you can use it directly. Its toString method is called only when tracing is enabled.

Using isTraceEnabled also useful if the string isTraceEnabled multiple trace statements. You can put them in the same if , then.

+4
source

While Henry's answer is correct, I would like to add that trace will actually have an isTraceEnabled call anyway, so there is actually a (very small) drawback to calling isTraceEnabled when it is not useful (i.e. you do not need to create parameters using non-trivial expressions, and you only have one trace call).

+1
source

Source: https://habr.com/ru/post/1399222/


All Articles