3

What is the best way to display the following details in org.slf4j.Logger

logger.info(problemCount + " problem entries deleted for the Object: " + id );

This is info, I want to display these finer details for a while to help with issues by looking at the logs.

I know there are some references that says built-in string formatting should be used instead of string concatenation like the following

logger.info(String.format("%d problem entries deleted for the Object: %s ",problemCount, id));

Is there an efficient way to write these kinds of log statements in java using org.slf4j.Logger?

Refer the link below as to why answer is preferred based on efficiency and performance.

Logger slf4j advantages of formatting with {} instead of string concatenation

Madhu Tomy
  • 662
  • 11
  • 25

2 Answers2

3

Yes, you can also do like this

logger.info( "{} problem entries deleted for the Object: {}",problemCount , id );
Rahul Kumar
  • 362
  • 1
  • 7
0

Firstly: SLF4J is:

The Simple Logging Facade 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 desired logging framework at deployment time.

So it is actually the implementation that is behind SLF4J that makes something more or less efficient.

But the whole point for this, using placeholder {} is that there is no evaluation made before the logger decides if anything is logged.

If you it like this:

logger.info(problemCount + " problem entries deleted for the Object: " + id );

the string concatenation is always made.

but doing it like:

logger.info( "{} problem entries deleted for the Object: {}",problemCount , id );

lets the logger decide if any concatenation ore more complex stuff is done.

So - for example - if you have log level warn and you log info there is no concatenation or any other evaluation done which makes the latter more efficient.

pirho
  • 11,565
  • 12
  • 43
  • 70