Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of curiosity -- why don't logging APIs implement printf()-like logging methods?

Tags:

java

logging

Whether it be java.util.logging, commons-logging, log4j, and even the most recent slf4j and logback, none of these APIs propose methods such as, say:

void debug(String format, Object... args);

where format is a string parsed using Formatter. Instead, they stick with MessageFormat.

I understand that it would conflict with existing prototypes, so why not debugf() or the like, but why aren't there any such methods given that Formatter is as old as Java 1.5?

Is it for performance reasons, backwards compatibility... ?

(note, I am really talking about Formatter, ie including placeholders such as %s, %02x etc)

like image 554
fge Avatar asked Dec 26 '11 12:12

fge


1 Answers

slf4j does. See http://www.slf4j.org/apidocs/org/slf4j/Logger.html. It's one of the main advantages (to me) of slf4j over log4j. It avoids the need for isDebugEnabled() everywhere.

EDIT :

The slf4j documentation explains why it doesn't use the standard formatting from the JDK:

SLF4J uses its own message formatting implementation which differs from that of the Java platform. This is justified by the fact that SLF4J's implementation performs about 10 times faster but at the cost of being non-standard and less flexible.

like image 170
JB Nizet Avatar answered Oct 16 '22 10:10

JB Nizet