Search code examples
javawindowstomcat7localejava-7

Locale <=> Time/Date format in Tomcat logs?


I can't seem to change the locale / date format in the Tomcat logs when running it from Eclipse. It's using a 12-hour clock and I want it to use a 24-hour clock instead (language is English, which already is what I want). Here's what I have tried so far:

  • Setting LANG and LC_ALL environment variables to en_GB in Tomcat's launch configuration
  • Adding -Duser.language=en -Duser.region=GB to VM options in Tomcat's launch configuration
  • Adding -Duser.language=en -Duser.region=GB to Eclipse's eclipse.ini
  • Setting -Duser.language=en -Duser.region=GB as default VM arguments in the JRE configuration

If I run Tomcat from the shell, it works as expected by adding -Duser.language=en -Duser.region=GB to %JAVA_OPTS% in bin\catalina.bat - as for example outlined here - but that doesn't affect Tomcat when it is running from within Eclipse, of course. (Erratum: I couldn't reproduce this and have to assume that I erroneously ran Tomcat with Java 6 instead of 7)

Used software: Tomcat 7.0.54, Oracle JDK 1.7.0_60 (64-bit), Windows 8 (64-bit)


Update: Thanks to Michael-O's answer, I was made aware of a change in Java's locale-resolution as of Java 7. Indeed, going back to Java 6 fixes my problem and gives me the 24-hour clock - but that is not feasible for my current project.

I've not been able to achieve the same with Java 7. Supposedly, you can return to the old locale-resolution by setting the system property sun.locale.formatasdefault to true, but that does not seem to affect the time display in Tomcat 7's logging at all. After a while I found this bug in which it's stated fairly clear:

(...) we lost the ability to decide on 24hour vs 12hour time depending on the locale, but we gained the ability to override the default format by providing a format string for the java.util.logging.SimpleFormatter.format property in logging.properties.

Thus, it seems, defining this format string manually is the (only?) way to go, and Michael-O also suggested that, so I'm accepting his answer.

After delving into the madness that is java.util.Formatter's syntax, I have for now settled with this format string, which needs to be put in the VM options in Tomcat's launch configuration:

-Djava.util.logging.SimpleFormatter.format="%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS_%1$tL %4$4.4s %3$s %5$s %n"

Output:

151003_195915_359 INFO org.apache.coyote.http11.Http11Protocol Starting ProtocolHandler ["http-bio-8080"] 
151003_195915_375 INFO org.apache.coyote.ajp.AjpProtocol Starting ProtocolHandler ["ajp-bio-8009"] 
151003_195915_375 INFO org.apache.catalina.startup.Catalina Server startup in 1280 ms

Alternatively shorter, for ISO 8601 timestamps use:

-Djava.util.logging.SimpleFormatter.format="%tFT%<tT.%<tL %4$4.4s %3$s %5$s %n"

Output:

2015-10-03T19:37:03.703 INFO org.apache.coyote.http11.Http11Protocol Starting ProtocolHandler ["http-bio-8080"] 
2015-10-03T19:37:03.703 INFO org.apache.coyote.ajp.AjpProtocol Starting ProtocolHandler ["ajp-bio-8009"] 
2015-10-03T19:37:03.703 INFO org.apache.catalina.startup.Catalina Server startup in 1189 ms 

Solution

  • You have probably been hit by this change in Java 7.

    The only solution I see is a custom formatter for ISO 8601.