javalogging

Why we use ::{} in java logger


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.


Solution

  • 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