[slf4j-dev] [Bug 50] New: I18N logging

bugzilla-daemon at pixie.qos.ch bugzilla-daemon at pixie.qos.ch
Fri Jun 1 09:58:04 CEST 2007


http://bugzilla.slf4j.org/show_bug.cgi?id=50

           Summary: I18N logging
           Product: SLF4J
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P3
         Component: Core API
        AssignedTo: dev at slf4j.org
        ReportedBy: sdavids at gmx.de


Currently all examples hard-code the logging strings, e.g.

logger.info("my text to be i18n {} {}", new Object[] { o1, o2 });

There should be a way to i18n these messages.

How does JDK-logging do it:

http://java.sun.com/javase/6/docs/api/java/util/logging/Logger.html#getLogger(java.lang.String,%20java.lang.String)

There should also be a way to change the underlying pattern parser.

Two alternative implementations:

1. http://java.sun.com/javase/6/docs/api/java/text/MessageFormat.html

2. http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax

Both offer reordering, using the same argument more than once, locale specific
dates/numbers/etc., and other useful features.

@@@@

In the meantime you should at least add a best practice of how to do I18N with
the SLF4J API to the FAQ; something along the line:

class MyClass {

  private final ResourceBundle bundle =
ResourceBundle.getBundle("my.package.messages");
  private final Logger logger =  LoggerFactory.getLogger(MyClass.class);

  void method() {

// we're just interested in Object#toString()
    logger.warn(bundle.getString("my_message_key"), new Object[] { 1, new
Date() });

// what if we want locale-specific logging?

// either (format the arguments outside the logging call):
    if (logger.isWarnEnabled()) {
      Format numberFormat = new DecimalFormat("00");
      Format dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
      logger.warn(bundle.getString("my_message_key"), new Object[] {
numberFormat.format(1), dateFormat.format(new Date()) });
    }

// or (do not use SLF4J API at all):
    if (logger.isWarnEnabled()) {
          MessageFormat format = new
MessageFormat(bundle.getString("my_message_key2"));
      logger.warn(format.format(new Object[] { new Date(), 1 }));
    }
  }
}

messages.properties

my_message_key=my text to be i18n {} {}
my_message_key2=my text to be i18n {1,date,short} {0,number,00}

@@@@@

If you want me to provide you with an initial implementation (of pattern parser
switching) say so.


-- 
Configure bugmail: http://bugzilla.slf4j.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.



More information about the slf4j-dev mailing list