Java 使 Logback 在其“%date”格式中包含日期和时间之间的“T”以严格符合 ISO 8601

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23096129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:40:25  来源:igfitidea点击:

Make Logback include the "T" between date and time in its "%date" format for strict ISO 8601 compliance

javalogback

提问by Basil Bourque

By default, the Logbackencoder uses a date formatsimilar to the ISO 8601standard. But it lacks the "T" in the middle between the date and time portions. The Tmakes for easier parsing, and is required by the standard (unless private parties agree otherwise).

默认情况下,Logback编码器使用类似于ISO 8601标准的日期格式。但它在日期和时间部分之间的中间缺少“T”。这使得解析更容易,并且是标准所要求的(除非私有方另有协议)。T

Is there some trick to get Logback to include the T?

有什么技巧可以让 Logback 包含T?

This…

这个…

2006-10-20T14:06:49,812

instead of this…

而不是这个……

2006-10-20 14:06:49,812

I suppose I could re-create the entire format while adding a "T", but I wonder if there is some simpler way.

我想我可以在添加“T”的同时重新创建整个格式,但我想知道是否有一些更简单的方法。

采纳答案by Sotirios Delimanolis

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.

Logback 的 JIRA 页面上有一个关于此的错误报告。从那以后就没有太大的发展24/Feb/10 3:57 PM。我只是为了引起注意而投票。你也应该。

I would provide my own date format that matches ISO 8601's.

我会提供与ISO 8601匹配的我自己的日期格式。

Insert "T"

插入“T”

This should do the trick:

这应该可以解决问题:

<pattern>%d{"yyyy-MM-dd'T'HH:mm:ss,SSS"} [%thread] %-5level %logger{35} - %msg %n
</pattern>

The ""are needed to make the ,work as described in the documentation.

""需要,使,工作的描述文档

That <pattern>element belongs in your Logback configuration setting. Here is an example logback.xmlfile:

<pattern>元素属于您的 Logback 配置设置。这是一个示例logback.xml文件:

<?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>

Time Zone

时区

To be more fully ISO 8601 compliant, and for more helpful logging, you should include a time zone.

为了更完全地符合 ISO 8601 并获得更有用的日志记录,您应该包括一个时区。

EDIT (Michael-O, 2014-06-15): That is nottrue, timezone is absolute optional.

编辑(Michael-O,2014-06-15):这不是真的,时区是绝对可选的。

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.

要包含时区,请将第二个参数(参见文档)传递给%date. 传递时区正确名称。避免使用三个或四个字母的时区代码,例如“EST”,因为它们既不唯一也不标准化。例如,通过Australia/Perth。通常对于日志记录,我们需要UTC(GMT) 时间,这意味着没有任何偏移。在这种情况下,通过UTC

You can display the time zone offset as number of hours and minutes as part of the date-time in the log. Append an Xto display the time zone offset as part of the date time value.

您可以将时区偏移显示为小时数和分钟数,作为日志中日期时间的一部分。附加X以将时区偏移显示为日期时间值的一部分。

This…

这个…

%date{"yyyy-MM-dd'T'HH:mm:ss,SSSXXX", UTC}

…produces…

……产生……

2014-04-16T09:59:24,009Z

The XXXworks in Java 7 and 8. In earlier versions of Java, you may be able to use a Zin the format definition to generate an offset number that lacks a colon.

XXXJava中7和8的作品在早期版本的Java,您可以使用Z格式定义中产生的偏移数量缺乏一个冒号。