首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Logrus时间戳格式

Logrus时间戳格式
EN

Stack Overflow用户
提问于 2016-03-25 01:17:01
回答 1查看 16.9K关注 0票数 19

我正在尝试从Golang日志包过渡到Logrus。我的问题是如何定制记录消息的时间戳格式。默认值是自启动以来的秒数,但我想要一个"2016-03-24 17:10:15“格式。我的简单测试代码是:

代码语言:javascript
复制
package main

import (
        "github.com/Sirupsen/logrus"
)

func main() {
        customFormatter := new(logrus.TextFormatter)
        customFormatter.TimestampFormat = "2006-01-02 15:04:05"
        logrus.SetFormatter(customFormatter)
        logrus.Info("Hello Walrus")
}

这段代码可以很好地编译和运行,但是时间戳格式没有改变。有人能提供一些关于它为什么不工作的见解吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-25 04:20:46

我相信您希望将以下字段设置为true,以便在自己运行时启用时间戳,并附加一个TTY。

logrus.TextFormatter文档中:

代码语言:javascript
复制
// Enable logging the full timestamp when a TTY is attached instead of just
// the time passed since beginning of execution.
FullTimestamp bool

调整您提供的示例:

代码语言:javascript
复制
package main

import (
    "github.com/Sirupsen/logrus"
)

func main() {
    customFormatter := new(logrus.TextFormatter)
    customFormatter.TimestampFormat = "2006-01-02 15:04:05"
    logrus.SetFormatter(customFormatter)
    logrus.Info("Hello Walrus before FullTimestamp=true")
    customFormatter.FullTimestamp = true
    logrus.Info("Hello Walrus after FullTimestamp=true")
}

产生:

代码语言:javascript
复制
$ go run main.go
INFO[0000] Hello Walrus before FullTimestamp=true
INFO[2016-03-24 20:18:56] Hello Walrus after FullTimestamp=true
票数 34
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36206187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档