[slf4j-dev] support for java.text.MessageFormat style formatting in SLF4J

Ceki Gulcu ceki at qos.ch
Fri Oct 16 10:31:51 UTC 2015


Hi all,

Over the years there have been several requests by users to support 
java.text.MessageFormat style formatting in SLF4J. Currently, SLF4J only 
supports its own style using accolades, as in

logger.info("User {} has logged on", userName);

whereas the java.text.MessageFormat style is more sophisticated. Here 
are two examples of logger calls using java.text.MessageFormat style

logger.info("User {1} has logged on", userName);
logger.info("Current temperature is {1, number, #.##} degrees", t);


SLF4J API does not support java.text.MessageFormat style because a 
significant proportion of projects using slf4j depend on external 
software. If two software components use different formatting styles for 
logging, then if any which one of the supported styles is chosen by the 
user via some configuration option, then logging statements based on the 
other style (the one not chosen by the user) will render as gibberish.

There is no way to get around this problem with a *global* configuration 
switch. A slightly different approach would be to apply one style on a 
subset of the logger statements in the project and apply another style 
on the rest. However, this would come at some computational cost and 
more importantly a high configuration cost paid by the unsuspecting 
end-user.

In my opinion, a better approach is to support both styles 
simultaneously. Looking at the message, it is easy to tell which style 
is desired by the user.

For example,

"Hello world"  // unparameterized
"User {} has logged on"  // slf4j-style
"User {1} has logged on" // j.t.M style
"Current temperature is {1, number, #.##} degrees" // j.t.M style

Coding the method returning the desired formatting style by logging at 
some string should not be hard. The only difficulty is doing so 
efficiently and at a very small computational cost.

Anyone interested in tackling this problem?

--
Ceki


More information about the slf4j-dev mailing list