我正在尝试从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")
}这段代码可以很好地编译和运行,但是时间戳格式没有改变。有人能提供一些关于它为什么不工作的见解吗?
谢谢
发布于 2016-03-25 04:20:46
我相信您希望将以下字段设置为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=truehttps://stackoverflow.com/questions/36206187
复制相似问题