为了账号安全,请及时绑定邮箱和手机立即绑定

Logrus 时间戳格式

Logrus 时间戳格式

Go
杨__羊羊 2021-12-27 16:05:45
我正在尝试从 Golang 日志包过渡到Logrus。我的问题是关于如何自定义记录消息的时间戳格式。默认值是自启动以来的秒数计数器,但我想要“2016-03-24 17:10:15”格式。我的简单测试代码是:package mainimport (        "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")}这编译并运行良好,但时间戳格式未更改。任何人都可以提供一些有关它为什么不起作用的见解吗?
查看完整描述

1 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

我相信您想将以下字段设置为 true 以在附加 TTY 的情况下自己运行时启用时间戳。


从logrus.TextFormatter文档:


// Enable logging the full timestamp when a TTY is attached instead of just

// the time passed since beginning of execution.

FullTimestamp bool

调整您提供的示例:


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")

}

产生:


$ go run main.go

INFO[0000] Hello Walrus before FullTimestamp=true

INFO[2016-03-24 20:18:56] Hello Walrus after FullTimestamp=true


查看完整回答
反对 回复 2021-12-27
  • 1 回答
  • 0 关注
  • 368 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号