Wednesday, July 12, 2006

Internationalization of logging and error messages for the server side

This is how one would throw an internationalizable message:
String msg = msgcat.get("EOpenFile", dir, file, ex);
throw new IOException(msg, ex);
The localized message is typically in a .properties file, e.g.
EOpenFile=F1231: Could not open file {1} in directory {0}: {2}
Each language has its own .properties file. The  msgcat class is a utility class that loads the .properties file. Logging messages to a log file typically uses the same constructs.

Looks cool, right? So what's my gripe?
  1. when coding this, you would have to update .properties file in addition to the .java file you're working on
  2. It's easy to make a typo in the message identifier,  EOpenFile in the example above; there is no compile time checking for these "constants".
  3. It's difficult to check that the right parameters are used in the right order ({0}, {1}) etc.
  4. When reviewing the .java file, it's difficult to check that the error message is used in the right context and that the error message is meaningful in the context.
  5. When reviewing the .properties files, it's difficult to determine where these error messages are used (if at all!) -- you can only find out through a full text search
Isn't there a better way?

No comments: