21

In log4j if we write

**logger.debug("Processing trade with id: " + id + " symbol: " + symbol);**

it will create String in string pool but when we use slf4j we use parameter based like this

**logger.debug("Processing trade with id: {} and symbol : {} ", id, symbol);**

So what is the difference between these two statement, slf4j will create String at run time or not ?

raw
  • 409
  • 1
  • 5
  • 11
  • [It could also be interesting that SLF4J is an abstraction layer. log4J is an implementation of it.][1] [1]: http://programmers.stackexchange.com/questions/108683/slf4j-vs-log4j-which-one-to-prefer – Olmo Rigolo Nov 28 '13 at 16:00

6 Answers6

23

The difference is increase of performance, in log4j the string is concatenated every time the line is evaluated even if log level is lower than debug so the string will never be used.

slf4j, the string and parameters are passed through to the logger which only substitutes them if the log message is actually to be used.

Imagine code with debug statements every few lines, when in production and debug is disabled that is a huge amount of string manipulation that will never be used.

dezzer10
  • 752
  • 7
  • 9
  • This was in 2014, but current Java the String when concatenated in code, ex "this" + "that";. it is compiled as an implementation of StringBuilder ex new StringBuilder("this").append("that").toString();. I think Java 8. – Delconis May 03 '18 at 22:51
  • 1
    @Delconis There is no "but" here, the answer is still very much true. Just _how_ the strings are concatenated does not matter, every time the logger is called, its parameter is evaluated, the strings concatenated and some small amount of work performed. If you only pass a String with {} and some references, the logger first checks whether the specified logging level is enabled and if not, it just doesn't do anything with the String at all. No concatenation ever happens, not even using comparatively quick StringBuilder. If you have tons of trace level debugging, it makes a big difference. – JohnEye Apr 24 '20 at 17:47
7

I would say to increase performance by reducing String concatenations.

When you write this

"Processing trade with id: " + id + " symbol: " + symbol

You are creating the printing string manually.

When you write

"Processing trade with id: {} and symbol : {} ", id, symbol
                    -------^id------------^symbol---------

In the second way before printing internally slf4j maintaind and generate a new string again with concatenation (Haven't check the source code,may be a StringBuilder).

The {} called as place holders and replace by the args passed by you.

From docs of sl4j

This form avoids superfluous string concatenation when the logger is disabled for the DEBUG level. However, this variant incurs the hidden (and relatively small) cost of creating an Object[] before invoking the method, even if this logger is disabled for DEBUG. The variants taking one and two arguments exist solely in order to avoid this hidden cost.

Read how to use the format :How to use java.String.format in Scala?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • so log4j+Stringbuilder can solve log4j multiple string creation problem – raw Oct 09 '13 at 11:44
  • @raw As I mentioned, I haven't checked with internal logic. 90% they might use StringBuilder to increase performance. And `so log4j+Stringbuilder can solve log4j multiple string creation problem` is true. – Suresh Atta Oct 09 '13 at 11:46
  • the main difference between log4j and slf4j is the creation of a string using String and StringBuilder.am i right ? – raw Oct 09 '13 at 12:15
  • Not exactly, The format you passed. In this case the place holders and see the method format() from string class. in java. See the attached link – Suresh Atta Oct 09 '13 at 12:38
7

SLF4J is basically an abstraction layer. It is not a logging implementation. It means that if you're writing a library and you use SLF4J, you can give that library to someone else to use and they can choose which logging implementation to use with SLF4J e.g. log4j or the Java logging API. It helps prevent projects from being dependent on lots of logging APIs just because they use libraries that are dependent on them.

Byorn
  • 681
  • 8
  • 5
2

With Log4j2 API, we can have logger.info("String: {} int : {}.", "Hello, World ", 10);

Sagar Mhatre
  • 489
  • 4
  • 8
1

So what is the difference between these two statement, slf4j will create String at rum time or not ?

The strings will be created anyways irrespective of whether you use log4j or sl4j. sl4j is offering a convenience of place holder.

Santosh
  • 17,667
  • 4
  • 54
  • 79
  • No, if the log level is coarser than debug, then the SLF4J version does not construct the concatenated string. – A_P Nov 05 '18 at 20:46
0

It is about String concatenation. First line always make String concat that expensive operation, second line is not concat if log level is not match debug. I am not sure only presume, matching log level second option can be better performance because of internal StringBuilder usage.

Erdinç Taşkın
  • 1,548
  • 3
  • 17
  • 28