Does log.debug decrease performance

I want to write some logs in the debug log that will not be available in production logs that are at the info log level. So how do these additional debug logs affect performance? I mean, if we set the log level to INFO, the logger has to check what the log level is and find that log.debug needs to be ignored.

So does this additional log level check affect performance?

Is there any automatic way to remove log.debug () statements during deployment? I mean during development the log.debug will be there and we can debug. But during production deployment, an automatic mechanism will remove all log.debug () messages. I'm not sure if this is possible.

+5


source to share


5 answers


So how do these extra debug logs affect performance?

This affects the performance of the application as registrars are disk I / O calls (assuming you are writing to the filesystem) and the level is not strictly recommended for production environments . P> DEBUG

Is there any automatic way to remove log.debug () statements when deploying?

No, there is no magic way to delete statements log.debug()

, BUT when you set the logging level to INFO, then as long as you are NOT doing heavy computation when passing parameters to a method debug()

, it should be okay. For example, if you have the logger level set to INFO and it is assumed that you have two loggers in your code :

logger.debug(" Entry:: "); //this logger is fine, no calculations
//Below logger, you are doing computations to print i.e., calling to String methods
logger.debug(" Entry : product:"+product+" dept:"+dept);//overhead toString() calls

      



I recommend using slf4j so you can avoid the overhead of a second logger using {}

(which is replaced with actual values ​​using MessageFormatter )
as shown below:

//Below logger product and dept toString() NOT invoked
logger.debug(" Entry : product:{} dept{}", product, dept);

      

Another important point is that with slf4j it is just an abstraction and you can switch between any logging frameworks , you can see below the text taken from here .

Simple Logical Phase for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j), allowing the end user to plug in the required framework log at deployment time.

+3


source


This can be minimized due to the way you record your logging statements. If you write

Object a = .... 
log.debug("I have an a: " + a);

      

then regardless of the logging structure you are using, the argument must be evaluated before running the debug function. This means that even if you are in the level INFO

, you pay the cost of making the call toString

to a

and create an argument string. If you instead write eg. (depending on what formatting your logging system uses, this works in log4j and slf4j)



log.debug("I have an a: {}", a);

      

you do not pay this cost, only the cost of the registrar checking if you are in DEBUG mode - if you don’t need this, you do not pay to evaluate the argument.

Another thing is to check that you are buffering the output (again, there are buffering applications in slf4j), which minimizes writes.

+3


source


You can wrap your "debug" statements when calling isDebugEnabled()

if (log.isDebugEnabled()) {
    log.debug("my debug statement");
}

      

Likewise, wrap your "information" statements on invocation isInfoEnabled()

, etc.

The idea behind this is that it is inexpensive (fixed cost) to check if the logging level is enabled. The cost of creating an operator that registers will depend on what you do.

+2


source


Another technique I would like to point out, often used in Android development, is that you can post-process your banner to remove calls like debug. The tool used is usually the protector. If you define the call as a side effect, it can be removed by the optimizer, which provides almost zero performance degradation ... it should even be smart enough to optimize whatever string construct you did for the log message.

https://www.guardsquare.com/en/proguard/manual/usage#assumenosideeffects

+2


source


The overhead of checking the logging level is very small, almost negligible. You will see a significant performance impact when you enable debug logs. The impact will depend on how much data you write to the logs, your storage (if your storage is an SSD, the performance hit is less compared to the performance you get with a regular disk), how many threads are written to the log (since only one thread can write to the file immediately, all other threads have to wait, and this is a sequential process). I've mentioned three, but there are several other factors that determine how much of an impact on application performance will have.

To answer your second question, there is no automatic way to remove debug instructions from your code.

+1


source







All Articles