0

I am using logback in our project. I have gone through the links for logback for curly braces.

logger.debug(" My class output value - {}, object toString() {}", object.value(), object.toString());

Debug is not enabled in my project. We saw the toString() is getting called in our project which impact our performance. Will the toString() be called during the code execution with debug disabled?

Can i use toString() with this approach? Because as per curly braces definition string concat will not happen. Will it be only for string concat or is that applicable for method calls too?

Shriram
  • 4,343
  • 8
  • 37
  • 64
  • 1
    Yes, it will be called; why wouldn't it be? The check for debug being enabled is in `debug`. – Dave Newton May 03 '18 at 17:21
  • Thanks. i didn't understand the last statement. – Shriram May 03 '18 at 17:22
  • 1
    `logger.debug()` calls `debug` with its parameters. Parameters are evaluated before functions are called. Inside `debug` it checks if debug-level logging is enabled. If it isn't, it returns, if it is, it logs whatever was passed in, e.g., the evaluated parameters. – Dave Newton May 03 '18 at 17:25
  • Ok. Which means that curly braces will avoid only string concat but the parameters will be evaluated irrespective of using curly braces or string concat operator(+) though debug is disabled. – Shriram May 03 '18 at 17:34

2 Answers2

1

From the LogBack documentation:

Better alternative

There exists a convenient alternative based on message formats. Assuming entry is an object, you can write:

Object entry = new SomeObject(); 
logger.debug("The entry is {}.", entry);

Only after evaluating whether to log or not, and only if the decision is positive, will the logger implementation format the message and replace the '{}' pair with the string value of entry. In other words, this form does not incur the cost of parameter construction when the log statement is disabled.

So, by passing just the object, without invoking toString(), you will save the toString() overhead.

Steve11235
  • 2,849
  • 1
  • 17
  • 18
1

Logback doesn't change the rules of Java: When you call a method, the arguments to the method need to be evaluated in order to be passed to the method. All you save by using the curly-brace notation when the log level isn't enabled is the cost of the String concatenation, that is to say, the cost of constructing the complete String to be logged from the individual components.

If the cost of evaluating the arguments is measurably decreasing your performance to the point where it no longer meets your customers' needs, then you probably want to avoid the cost of running it if the log level isn't enabled. From the Logback Manual, Chapter 2: Architecture, "Parameterized logging":

One possible way to avoid the cost of parameter construction is by surrounding the log statement with a test. Here is an example.

if(logger.isDebugEnabled()) { 
   logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}

This way you will not incur the cost of parameter construction if debugging is disabled for logger. On the other hand, if the logger is enabled for the DEBUG level, you will incur the cost of evaluating whether the logger is enabled or not, twice: once in debugEnabled and once in debug. In practice, this overhead is insignificant because evaluating a logger takes less than 1% of the time it takes to actually log a request.

Using the curly-brace syntax (presented shortly after in the manual) often is a good compromise, and I really prefer it just because it helps distinguish between the statement being logged and the data that goes into it. But it isn't quite the same as being skipped entirely if your log level isn't enabled, because the parameters are still evaluated and passed to the logging system before it can figure out whether it needs to log them or not.