我正在尝试从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")
}
这编译并运行正常,但时间戳格式保持不变.任何人都可以提供一些有关它不工作原因的见解吗?
谢谢
小智 19
我相信您要将以下字段设置为true,以便在连接TTY时自行运行时启用时间戳.
// 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