2

I'm using java.util.logging in Android because I need to log to other outputs than the system logs. At the moment I'm using a java.util.logging.SimpleFormatter. When it logs to a file, it formats the date and timestamp using a default locale, which in my case makes it difficult to read (non-European numerals, complicated use case, long story).

In java.util.Formatter (as opposed to java.util.logging.Formatter), you can supply a Locale argument to the constructor, so you can specify the ROOT locale if you want:

Formatter formatter = new Formatter(sb, Locale.ROOT);

(In a similar way you can pass a locale argument to String.format().) But java.util.logging.Formatter and its descendants don't seem to have any way to specify the locale used.

I could extend Formatter or SimpleFormatter and override format() or formatMessage(), but I don't see any hooks in there for formatting just the date/time stamp, so I'd have to rewrite the functionality of formatting the whole LogRecord. That's probably not too bad, but it's a lot less optimal (and more error-prone) than supplying a locale.

My current strategy is to copy-and-modify the code for SimpleFormatter.format(), but of course that cuts the app off from future fixes to that method.

Any other ideas?

jmehrens
  • 10,580
  • 1
  • 38
  • 47
LarsH
  • 27,481
  • 8
  • 94
  • 152

1 Answers1

1

You can change the format of the SimpleFormatter to any pattern supported by the java.util.Formatter. As you point out, the Locale.getDefault() is always used by the SimpleFormatter to format log records.

Try using the java.util.Locale.setDefault​(Locale.Category, Locale) to set the format category to Locale.ROOT. For example:

java.util.Locale.setDefault​(Locale.Category.FORMAT, Locale.ROOT);

Otherwise, you have to try to format individual parts of the date as described in java.util.logging.SimpleFormatter documentation:

java.util.logging.SimpleFormatter.format="%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%n"

Disclaimer: I'm a content developer for com.sun.mail.util.logging included with JavaMail project. If you use the com.sun.mail.util.logging.CompactFormatter it chooses the locale of the resource bundle assigned to the log record. If you are not using localization then null is used as the default locale which should be the same as the ROOT.

As a last resort you'll have to roll your own or find a 3rd party lib.

jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • Thanks, changing the default locale is a good idea for how to make SimpleFormatter use the locale I want. However, in this case, setting the default locale would risk ripple effects that I don't want to happen, such as changing the current layout direction. – LarsH Apr 02 '18 at 16:38
  • 1
    @LarsH It might be time to file a RFE against OpenJDK to improve the locale handling in logging. I really think it should use the locale of the resource bundle but, allowing the user to choose the locale as a logging property is a good idea too. If you have access to JavaMail you might be able to use the CompactFormatter with a custom pattern. – jmehrens Apr 02 '18 at 18:01