Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do logger always read debug even if level is set to info and does it have any significant impact?

Tags:

java

logging

As the title say. If I have a

LOGGER.debug(model.toString())

and I set the logging level to INFO, will this row always be executed and if so, is it a big impact if you have alot of debug logging? I've seen a few applications that have a property called debug=true and in the code the type

if(debug) {
    LOGGER.debug("log message");
}

Is this a better approach or should I just continue to use LOGGER.debug() without the proeprty check?

like image 217
Lundell Avatar asked Feb 16 '18 14:02

Lundell


People also ask

When would you use a debug logger?

If you want to print the value of a variable at any given point, you might call Logger. debug . This combination of a configurable logging level and logging statements within your program allow you full control over how your application will log its activity.

Is debug log important?

DEBUG logging level is used to fetch information needed to diagnose, troubleshoot, or test an application. This ensures a smooth running application.

What is debug logging level?

A debug level is a set of log levels for debug log categories, such as Database , Workflow , and Validation . A trace flag includes a debug level, a start time, an end time, and a log type. The log types are DEVELOPER_LOG , USER_DEBUG , and CLASS_TRACING .

Does logging level affect performance?

Log levels can be set for various parts of the system. In normal operation the log level for all of the subsystems should be set to INFO. DEBUG level can adversely impact performance.


2 Answers

Logging classes are just normal Java classes.

If you have a method call in your code, it will be called.

However… all logging frameworks’ log methods are designed to return immediately if the relevant level is not enabled, which makes the entire method call have so little cost it’s essentially nothing. For that reason, you almost never should use if (debug) or if (logger.isDebugEnabled()). They aren’t saving any time at all.

However, normal Java methods are still normal Java methods. When you execute this:

LOGGER.debug(model.toString());

Java will first invoke model.toString(), then pass the result of that to the logger. Unlike the logger call, that toString can be expensive, if the toString method does a lot of work and/or is called very frequently. To address this, logging frameworks have parameterized log methods, which will convert arguments to strings only if the relevant level is enabled.

Using java.util.logging.Logger, it looks like this:

logger.log(Level.FINE, "Model={0}", model);     // no toString() call

Using SLF4J or Log4j:

logger.debug("Model={}", model);                 // no toString() call
like image 141
VGR Avatar answered Oct 24 '22 11:10

VGR


This has not so much to do with loggers, but everything with how expressions in Java are evaluated. Contrary to for example Haskell, which does lazy evaluation, all expressions in a parameter list in Java are always evaluated, with the exception of Boolean short-cut.

So, if you do this:

LOGGER.debug(model.toString());

this is always evaluated, regardless of your logging level. This is why you should always use this form:

if (LOGGER.isDebugEnabled()) {
    LOGGER.debug(model.toString());
}

The impact of the call to isDebugEnabled() is negligible.

The one time that Java stops evaluating is if a Boolean expression is already determined to be false, so this will not thrown an exception:

if (1 == 0 && 0 / 0 > 0) {
    System.out.println("Hello, world!");
}
like image 37
SeverityOne Avatar answered Oct 24 '22 13:10

SeverityOne