【问题标题】:Logrus timestamp with millisecond以毫秒为单位的 Logrus 时间戳
【发布时间】:2019-11-07 16:38:27
【问题描述】:

使用最新版本的logrus (v1.4.2),我无法在日志格式化程序中设置毫秒/微秒。

与之前的版本(我不记得是哪个),我只是使用以下时间格式:

Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = "15-01-2018 15:04:05.000000"
Formatter.FullTimestamp = true
Formatter.ForceColors = true
log.SetFormatter(Formatter)
log.SetLevel(log.DebugLevel)

不幸的是,在最新版本中,似乎不再允许这种时间戳格式。
当我尝试使用上述时间格式打印一行时,我收到以下结果:
17-11-7118 17:35:46.314715

而不是正确的:
7-11-2019 17:35:46.314715

有人知道如何配置Logrus微秒/毫秒 精度打印时间戳吗?

【问题讨论】:

  • 您的格式字符串无效。它没有使用正确的reference time
  • 我也看不出这与毫秒有何关系。你的毫秒工作完美。
  • 感谢您的解决方案。如您所见,如果我设置特定的时间格式(17-11-7118 而不是 7-11-2019),第一个是不正确的

标签: go logging


【解决方案1】:

正如@Flimzy 所指出的,我使用的时间格式无效。

我已修复使用以下时间格式2006-01-02T15:04:05.999999999Z07:00

另一种(更人性化的)时间戳格式可以是:Jan _2 15:04:05.000000000

有关时间格式的更多示例,请参阅此处-> https://golang.org/pkg/time/#pkg-constants

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 2015-06-17
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多