Mohammad Roohitavaf Mohammad Roohitavaf - 3 years ago 112
Java Question

Does logging have significant overhead, even when it is OFF?

I want to add detailed logging information using fine/finer/finest to my code to help me debugging my code. A nice thing (and maybe the whole point) of using logger instead of System.out.println() is that I can flexibly change the level of information that I want to see. Finally, once I am done with debugging, I can set my logging to OFF or SEVERE.

But I am concern, that even if I set my logging to OFF, the extensive use of logging messages, may impact the performance of my application at production time. Is it a point of concern? Or java logging is designed in a way that by setting logging level to OFF all overhead of logging is gone?

Note that even if we don't concatenate string for log message, we may still face performance overhead if we use it a lot.


Answer Source

No, there is no significant overhead.

You mention in a comment that you’re using Java SE’s logging package. From the java.util.logging.Logger documentation:

On each logging call the Logger initially performs a cheap check of the request level (e.g., SEVERE or FINE) against the effective log level of the logger. If the request level is lower than the log level, the logging call returns immediately.

In other words, the log methods all return immediately if the level isn’t a loggable level. The “overhead” of merely calling a method that returns so quickly would be measured in nanoseconds, if it’s measurable at all.

Unnecessary string concatenation, instead of proper parameterized logging, can create performance issues, but you already seem to be aware of that. (I’m mentioning it for other readers who may not be aware of the issue.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download