从 Eclipse 运行时,我似乎无法更改 Tomcat 日志中的区域设置/日期格式。它使用 12 小时制,我希望它使用 24 小时制(语言是英语,这已经是我想要的)。到目前为止,这是我尝试过的:
- 在 Tomcat 的启动配置中将
LANG
和LC_ALL
环境变量设置为en_GB
- 将
-Duser.language=en -Duser.region=GB
添加到 Tomcat 启动配置中的 VM 选项 - 将
-Duser.language=en -Duser.region=GB
添加到 Eclipse 的eclipse.ini
- 将
-Duser.language=en -Duser.region=GB
设置为 JRE 配置中的默认 VM 参数
如果我从 shell 运行 Tomcat,它会通过将 (勘误表:我无法重现这个,不得不假设我错误地使用 Java 6 而不是 7 运行了 Tomcat )-Duser.language=en -Duser.region=GB
添加到 %JAVA_OPTS%
来按预期工作在 bin\catalina.bat
中 - 例如 outlined here - 当然,当 Tomcat 从 Eclipse 中运行时,这不会影响它。
使用的软件:Tomcat 7.0.54
、Oracle JDK 1.7.0_60
(64 位)、Windows 8(64 位)
更新:感谢Michael-O的回答,我意识到 Java's locale-resolution 的变化从 Java 7 开始。确实,回到 Java 6 解决了我的问题并为我提供了 24 小时时钟 - 但这对于我当前的项目来说是不可行的。
我无法使用 Java 7 实现相同的效果。据说,您可以通过将系统属性 sun.locale.formatasdefault
设置为 true 来返回到旧的语言环境分辨率
,但是这似乎根本不会影响 Tomcat 7 日志记录中的时间显示。过了一会儿我找到了this bug其中说得很清楚:
(...) 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.
因此,似乎手动定义此格式字符串是(唯一的?)方法,Michael-O 也建议这样做,所以我接受他的回答。
在深入研究了 java.util.Formatter
's syntax 的疯狂之后,我现在已经确定了这个格式字符串,它需要放在 Tomcat 启动配置的 VM 选项中:
-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"
输出:
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
或者更短,对于 ISO 8601时间戳使用:
-Djava.util.logging.SimpleFormatter.format="%tFT%<tT.%<tL %4$4.4s %3$s %5$s %n"
输出:
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
最佳答案
你可能被this击中了Java 7 中的更改。
我看到的唯一解决方案是 ISO 8601 的自定义格式化程序。
关于java - Locale <=> Tomcat 日志中的时间/日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32862846/