Phuthib
Phuthib

Reputation: 1436

Java logging performance

I am using wildfly21 set to log level=INFO. In the deployed code there are a lot of logger.debug statements.

e.g

private static Logger logger = LogManager.getLogger(getClass.getName());
logger.debug("Some debug message");

The debug statements are correctly not being logged to file as the logging level is set to INFO.

My question is around the performance cost of logger.debug.

Is there any performance cost in keeping the debug statements in code. Or is the real cost in the File I/O and as such there is no harm in leaving the debug statements for troubleshooting purposes when needed.

Upvotes: 0

Views: 1020

Answers (1)

DuncG
DuncG

Reputation: 15136

When your logging call is low cost such as with string literal (or the String format variant suggested by @markspace) you can use logger.debug as you have above:

logger.debug("Some debug message");

However if the statement has a side effect cost such as:

logger.debug("Value of X="+x+" extra bits "+someSideEffect(y));

... then always prefix with a check on logging level before use. Most Logger APIs support isXYZEnabled or logger.isEnabled(Level.XYZ) to determine whether the log message is used:

if(logger.isDebugEnabled())
    logger.debug("Value of X="+x+" extra bits "+someSideEffect(y));

This avoids any possible impact from the unnecessary String creation and other computations performed by the unused logging levels, and there is no need to comment out debug statements when info level logging is used.

Upvotes: 2

Related Questions