英文:
Go logger to print timestamp
问题
将时间戳打印到日志中,你可以使用log
包的SetFlags
函数来配置。以下是修改后的代码示例:
l := log.New(os.Stdout, "[AAA] ", log.LstdFlags)
l.Printf("Listening on %s", addr)
这将打印出类似于2014-09-15 10:23:12 [AAA] Listening on ...
的日志信息。log.LstdFlags
参数用于设置时间戳的格式。
英文:
Go logger to print timestamp
I have the following:
l := log.New(os.Stdout, "[AAA] ", 2)
l.Printf("Listening on %s", addr)
This prints out [AAA] Listening on ~
Is there any way that I can configure log
package to print out
2014-09-15 10:23:12 [AAA] Listening on ...
?
答案1
得分: 6
要获得您特定请求的输出,您可以将其包装在自己的Log
函数中,并每次使用SetPrefix
设置前缀(或在自己的类型中嵌入一个记录器,并使用另一个函数进行扩展):
func Log(l *log.Logger, msg string) {
l.SetPrefix(time.Now().Format("2006-01-02 15:04:05") + " [AAA] ")
l.Print(msg)
}
例如:
l := log.New(os.Stdout, "", 0)
Log(l, "Log 1")
<-time.After(time.Second * 3)
Log(l, "Log 2")
在我的机器上输出如下:
2014-10-02 11:12:14 [AAA] Log 1
2014-10-02 11:12:17 [AAA] Log 2
请注意,log
包有一些预定义的标志可供使用,但它们不能产生您在问题中请求的格式。要完全按照您的要求进行设置,您必须将标志设置为零,并自己完成设置。
英文:
To get your specific requested output, you can wrap it in your own Log
function and set the prefix each time using SetPrefix
(or embed a logger in your own type and extend it with another function):
func Log(l *log.Logger, msg string) {
l.SetPrefix(time.Now().Format("2006-01-02 15:04:05") + " [AAA] ")
l.Print(msg)
}
For example:
l := log.New(os.Stdout, "", 0)
Log(l, "Log 1")
<-time.After(time.Second * 3)
Log(l, "Log 2")
..outputs this on my machine:
2014-10-02 11:12:14 [AAA] Log 1
2014-10-02 11:12:17 [AAA] Log 2
Note that the log
package has some predefined flags that you can use, however they don't produce the format you've requested in your question. To get it exactly like that, you have to pass zero for the flags and do it yourself.
答案2
得分: 6
l := log.New(os.Stdout, "[AAA] ", 2)
l.Printf("Listening on %s", addr)
打印出[AAA] 17:07:51 Listening on ...
如果你想添加日期,可以使用log.Ldate | log.Ltime
作为标志:
l := log.New(os.Stdout, "[AAA] ", log.Ldate | log.Ltime)
l.Printf("Listening on %s", addr)
// [AAA] 2016-07-14 17:07:51 Listening on ...
但是如果你想在前缀之前添加日期和时间,你可以使用自定义的写入器:
type writer struct {
io.Writer
timeFormat string
}
func (w writer) Write(b []byte) (n int, err error) {
return w.Writer.Write(append([]byte(time.Now().Format(w.timeFormat)), b...))
}
然后,将自定义的写入器传递给日志的New
函数:
l := log.New(&writer{os.Stdout, "2006-01-02 15:04:05 "}, "[AAA] ", 0)
l.Printf("Listening on %s", addr)
// 2016-07-14 17:07:51 [AAA] Listening on ...
这里有一个完整的示例链接。
英文:
l := log.New(os.Stdout, "[AAA] ", 2)
l.Printf("Listening on %s", addr)
Prints out [AAA] 17:07:51 Listening on ...
If you want to add the date, use log.Ldate | log.Ltime
as flag:
l := log.New(os.Stdout, "[AAA] ", log.Ldate | log.Ltime)
l.Printf("Listening on %s", addr)
// [AAA] 2016-07-14 17:07:51 Listening on ...
But for having the date and time before prefix, you can use a customized writer:
type writer struct {
io.Writer
timeFormat string
}
func (w writer) Write(b []byte) (n int, err error) {
return w.Writer.Write(append([]byte(time.Now().Format(w.timeFormat)), b...))
}
Then, pass the custom writer to log's New
:
l := log.New(&writer{os.Stdout, "2006-01-02 15:04:05 "}, "[AAA] ", 0)
l.Printf("Listening on %s", addr)
// 2016-07-14 17:07:51 [AAA] Listening on ...
There is a complete example here.
答案3
得分: 3
设置日志标志log flags:
log.SetFlags(log.LstdFlags)
答案4
得分: 3
不确定是何时添加的,但你也可以直接设置Lmsgprefix
标志。
Lmsgprefix // 将“前缀”从行首移到消息之前
例如:
log.SetFlags(log.Lmsgprefix | log.LstdFlags)
英文:
Not sure when it was added but you can also just set the Lmsgprefix
flag
Lmsgprefix // move the "prefix" from the beginning of the line to before the message
e.g.:
log.SetFlags(log.Lmsgprefix | log.LstdFlags)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论