使用Go语言的日志记录器打印时间戳。

huangapple go评论85阅读模式
英文:

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(&quot;2006-01-02 15:04:05&quot;) + &quot; [AAA] &quot;)
    l.Print(msg)
}

For example:

l := log.New(os.Stdout, &quot;&quot;, 0)

Log(l, &quot;Log 1&quot;)

&lt;-time.After(time.Second * 3)

Log(l, &quot;Log 2&quot;)

..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, &quot;[AAA] &quot;, 2)
l.Printf(&quot;Listening on %s&quot;, 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, &quot;[AAA] &quot;, log.Ldate | log.Ltime)
l.Printf(&quot;Listening on %s&quot;, 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(&amp;writer{os.Stdout, &quot;2006-01-02 15:04:05 &quot;}, &quot;[AAA] &quot;, 0)

l.Printf(&quot;Listening on %s&quot;, addr)
// 2016-07-14 17:07:51 [AAA] Listening on ...

There is a complete example here.

答案3

得分: 3

设置日志标志log flags

log.SetFlags(log.LstdFlags)
英文:

set go log flag 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 &quot;prefix&quot; from the beginning of the line to before the message

e.g.:

log.SetFlags(log.Lmsgprefix | log.LstdFlags)

huangapple
  • 本文由 发表于 2014年10月2日 09:01:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/26152993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定