5

Hi I have a question regarding a log4j logger.

There is a part of the logger text I don't quite follow. The line looks as follows

logger.debug("Some text goes here [{}] and some more text here", someObject.function());

I had a look but I can't seem to find this as a regular expression, it smells strongly like it could be though and I suspect that it will be replaced by the returned value of someObject.function() much how one could replace string values in output by placing a %s. Would I be correct in this assumption? if not please elaborate as to what it does.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142

2 Answers2

2

It is not a regular expression. {} is a special syntax to print out objects and which is preferable over string concatenation. For example,

logger.debug("{}, {}", o1, o2); // 2 objects will be printed
logger.debug(o1 + ", " + o2); // the same in concatenation style

I suggest reading about advantages of formatting with {} instead of string concatenation.

I would like to add that most of the logging systems support asynchronous logging parameterized messages, it means you will always get an actual value (it doesn't guarantee by concatenation).

Community
  • 1
  • 1
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
1

The value of someObject.function() will get replaced in {}

rajesh
  • 3,247
  • 5
  • 31
  • 56