[slf4j-dev] [Bug 116] New: Switching the log message format to that of java.util. Formatter
bugzilla-daemon at pixie.qos.ch
bugzilla-daemon at pixie.qos.ch
Tue Nov 25 15:04:13 CET 2008
http://bugzilla.slf4j.org/show_bug.cgi?id=116
Summary: Switching the log message format to that of
java.util.Formatter
Product: SLF4J
Version: unspecified
Platform: All
OS/Version: All
Status: NEW
Severity: normal
Priority: P3
Component: Core API
AssignedTo: dev at slf4j.org
ReportedBy: grzegorzborkowski at o2.pl
I hate tons of "isDebugEnabled" in the code, as I believe the construction of
message should the the task of logging library. For a long time I missed the
feature of parametrized logging in Log4J and java.util.Logging. The fact that
Java 5 and Java 6 did not take advantage of new java.util.Formatter class to
introduce parametrized logging in JUL is another nail in the JUL's coffin. So I
was really glad to find the parametrized logging in SL4J and this in fact made
me switch to SLF4J.
However, the message format used by SL4J ("some message with param1 {} and
param2 {}") has 2 limitations:
1. You cannot specify the conversion/formatting of the parameters. If the param
is a date, you have no way to specify the formatting. The same with any
non-integer numbers.
2. The order of parameters is fixed. If you want to internationalize logging
message (not very common, but it sometimes is required, especially for error
messages) it can happen that in different language the parameters must be used
in different order. You cannot do it with current SL4J version.
So my proposal is to use formatting provided by java.util.Formatter class. The
message syntax used by this class is quite mysterious at first, but after
"breaking the ice" it shows up to be very powerful. SL4J could either use
java.util.Formatter internally, or, if backward compatibility with pre-Java 5
must be kept, one can implement formatter using the same formatting strings.
The Formatter class message syntax solves both problems mentioned above:
1. You can specify the date/time/number and other formatting, e.g. "some
message with param1 %td and param2 %tc"
2. You can change the parameters order in internationalized messages, e.g.:
"translated message with param2 %2$s first and param1 %1$s second". You can
also change the parameters formatting in internationalized messages.
As I said, the format is a bit awkward, but: 1) you can get used to it (at
least I did, after some time); 2) it is really powerful, you can specify
conversions without additional lines of codes 3) it is standard Java stuff
since Java 5, so you can expect that many/most people should be familiar with
it.
Note: The java.text.Format class uses the message format similar to current
SL4J format (with curly braces {} ) but it reaquires programmating creation of
formatter, setting format etc, so it is unusable in this context (I don't want
to put several lines of code guarded by "isDebugEnabled" again to print some
message; besides in internationalized message you wouln't be able to change the
format).
--
Configure bugmail: http://bugzilla.slf4j.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.
More information about the slf4j-dev
mailing list