3

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 
zb226
  • 9,586
  • 6
  • 49
  • 79
  • 1
    Thanks for the additional investigation and the ticket link. JUL remains a pain in the ass. One recommendation though: rather use the following formatter `%tFT% – Michael-O Oct 02 '15 at 19:49
  • I actually like my timestamps that way, but I know that's a bit eccentric :) Included your version above! – zb226 Oct 02 '15 at 23:47
  • 1
    Guess what? I have just tried to pass that property to Tomcat in `setenv.sh`, `catalina.sh`, even directly to the Java command in `catalina.sh`, I simply cannot properly escape the spaces in the property value. This JUL shit drives me crazy, I have wasted two hours for this lousy task without result. I will evaluate: https://github.com/grgrzybek/tomcat-slf4j-logback – Michael-O Oct 15 '15 at 21:35
  • 1
    Here is a followup: http://stackoverflow.com/q/11308633/696632 – Michael-O Oct 15 '15 at 21:51

2 Answers2

2

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

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

ricmarques
  • 131
  • 1
  • 7
Michael-O
  • 18,123
  • 6
  • 55
  • 121
  • Thanks, this is definitely the root cause! Going back to Java 6 (which I can't for this project) it works again. I'm still in the process of finding a solution... – zb226 Oct 01 '15 at 09:32
  • @zb226, I am also pissed by that. The only solution I see is a custom formatter for ISO 8601. Why don't you issue a bug report with Tomcat to use ISO 8601 in the log output by default? – Michael-O Oct 01 '15 at 09:42
  • I might just do that, but I need to fully understand first. How can it be that it still works from the command-line? This suggests to me Eclipse being the culprit, not Tomcat. – zb226 Oct 01 '15 at 12:10
  • 1
    @zb226 You can set `-Dsun.locale.formatasdefault=true` to restore the previous setting. Consider that Eclipse forks the VM and passes probably other properties in contrast to the command line. – Michael-O Oct 01 '15 at 13:36
  • Yea, I've read that, but tried it to no avail - in `eclipse.ini` as well as `_?JAVA_OPT(ION)?S` environment variables... – zb226 Oct 01 '15 at 13:41
  • @zb226 In the Run configuration of your Tomcat instance in Eclipse. – Michael-O Oct 01 '15 at 18:48
  • 1
    Of course I had tried this as well. It all doesn't work. See my update for a new piece of information, suggesting why this cannot work at the moment. – zb226 Oct 02 '15 at 14:06
1

Hope you need to change the date format in catalina.out. Please modify your logging.properties as below.

For org.apache.juli.OneLineFormatter:

java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
org.apache.juli.OneLineFormatter.timeFormat = yyyy-MM-dd HH:mm:ss

For java.util.logging.SimpleFormatter:

java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format = "%4$s: %5$s [%1$tc]%n"
StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
Athi
  • 61
  • 1
  • 4