Logrus 时间戳格式

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


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

}

这编译并运行良好,但时间戳格式未更改。任何人都可以提供一些有关它为什么不起作用的见解吗?


杨__羊羊
浏览 369回答 1
1回答

凤凰求蛊

我相信您想将以下字段设置为 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 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 before FullTimestamp=true")    customFormatter.FullTimestamp = true    logrus.Info("Hello Walrus after FullTimestamp=true")}产生:$ go run main.goINFO[0000] Hello Walrus before FullTimestamp=trueINFO[2016-03-24 20:18:56] Hello Walrus after FullTimestamp=true
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go