-3

I saw in code some programmer write this in code while log something.

I tried to google why we use ::{} in logger i did not find. Can some share 1-why we use it 2- where should use and where should not.

iTech
  • 13
  • 1
  • 8
  • please provide the entire example usage – Michael Markidis May 15 '17 at 05:25
  • can you please show the full line so one can have the context before answering ? – Mritunjay May 15 '17 at 05:25
  • http://stackoverflow.com/questions/10555409/logger-slf4j-advantages-of-formatting-with-instead-of-string-concatenation – Sagar Gandhi May 15 '17 at 05:28
  • Possible duplicate of [Logger slf4j advantages of formatting with {} instead of string concatenation](http://stackoverflow.com/questions/10555409/logger-slf4j-advantages-of-formatting-with-instead-of-string-concatenation) – mkilmanas May 15 '17 at 07:12

1 Answers1

3

I believe you are referring to message formatter. So the {} are like placeholder. In Java there is http://docs.oracle.com/javase/8/docs/api/java/text/MessageFormat.html for formatting. So the idea behind is that it allows you to format a string without using messy string concatenation. Assuming there was no such way then you would have to do

"I have" + variable1 + " and " + variable2

but now instead you can do

"I have {0} and {1} and also {0}", variable1, variable2

Notice that I can even reuse the same placeholder multiple times. In addition you can do Number, Date Formatting etc.

Hope that helps

tabiul
  • 607
  • 3
  • 8
  • 17