使Logback包含“T”和“T”。在“%date”之间的日期和时间之间严格符合ISO 8601标准的格式 [英] Make Logback include the "T" between date and time in its "%date" format for strict ISO 8601 compliance
问题描述
默认情况下, Logback 编码器使用日期格式类似于 ISO 8601 标准。但它在日期和时间部分之间缺少T。 T
便于解析,并且是标准所要求的(除非私人方另有约定)。
By default, the Logback encoder uses a date format similar to the ISO 8601 standard. But it lacks the "T" in the middle between the date and time portions. The T
makes for easier parsing, and is required by the standard (unless private parties agree otherwise).
是还有一些技巧让Logback包含 T
?
Is there some trick to get Logback to include the T
?
这...
2006-10-20T14:06:49,812
而非此......
2006-10-20 14:06:49,812
我想我可以在添加T时重新创建整个格式,但我想知道是否有更简单的方法。
I suppose I could re-create the entire format while adding a "T", but I wonder if there is some simpler way.
推荐答案
错误报告
Logback的JIRA页面。自 24 / Feb / 10 3:57 PM
以来,没有太多发展。我刚投票吸引了注意力。你也应该这样。
Bug Report
There's a bug report about this on Logback's JIRA page. There hasn't been much development since 24/Feb/10 3:57 PM
. I've just voted to attract attention. You should too.
我会提供我自己的日期格式 ISO 8601 。
I would provide my own date format that matches ISO 8601's.
这应该是技巧:
<pattern>%d{"yyyy-MM-dd'T'HH:mm:ss,SSS"} [%thread] %-5level %logger{35} - %msg %n
</pattern>
需要来制作
,
按照文档。
< pattern>
元素属于您的Logback配置设置。下面是一个示例 logback.xml
文件:
That <pattern>
element belongs in your Logback configuration setting. Here is an example logback.xml
file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<!-- Strangely, Logback lacks a built-in formatter for ISO 8601. So, roll our own. -->
<Pattern>%date{"yyyy-MM-dd'T'HH:mm:ss,SSSXXX", UTC} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<logger name="com.example" level="TRACE"/>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
时区
更全面符合ISO 8601标准,对于更有帮助的日志记录,您应该包含一个时区。
Time Zone
To be more fully ISO 8601 compliant, and for more helpful logging, you should include a time zone.
编辑(Michael-O,2014-06-15):那是不是,时区是绝对可选的。
EDIT (Michael-O, 2014-06-15): That is not true, timezone is absolute optional.
要包含时区,请传递第二个参数(见文档)到%date
。传递时区的正确名称。避免使用三个或四个字母的时区代码,例如EST,因为它们既不是唯一的也不是标准化的。例如,传递 Australia / Perth
。通常,对于日志记录,我们需要 UTC (GMT)时间,这意味着没有任何抵消。在这种情况下,传递 UTC
。
To include a time zone, pass a second argument (see doc) to the %date
. Pass the proper name of a time zone. Avoid the three or four letter time zone codes such as "EST" as they are neither unique nor standardized. For example, pass Australia/Perth
. Generally for logging we want UTC (GMT) time, meaning without any offset. In that case, pass UTC
.
您可以将时区偏移量显示为小时和分钟数作为一部分日志中的日期时间。附加 X
以显示时区偏移量作为日期时间值的一部分。
You can display the time zone offset as number of hours and minutes as part of the date-time in the log. Append an X
to display the time zone offset as part of the date time value.
此...
%date{"yyyy-MM-dd'T'HH:mm:ss,SSSXXX", UTC}
...生产...
2014-04-16T09:59:24,009Z
XXX
适用于Java 7和8.在早期版本的Java中,您可以在格式定义中使用 Z
来生成缺少a的偏移号冒号。
The XXX
works in Java 7 and 8. In earlier versions of Java, you may be able to use a Z
in the format definition to generate an offset number that lacks a colon.
这篇关于使Logback包含“T”和“T”。在“%date”之间的日期和时间之间严格符合ISO 8601标准的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!