Is there a way to append io.multiwriter with timestamp in go?

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

Is there a way to append io.multiwriter with timestamp in go?

问题

我正在处理使用Go生成的日志。日志内容被打印到stdout和一个日志文件中,使用了io.MultiWriterlog := io.MultiWriter(os.Stdout, logfile)。我想要给stdout和日志文件都添加时间戳。

我的方法是编写一个写入函数:

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...))
}
log := io.MultiWriter(&writer{os.Stdout, "2006/01/02 15:04:05"}, logFile)

然而,这样做并没有在stdout和日志文件中产生时间戳。有没有人知道是否有其他方法可以实现这个目的?

英文:

I am dealing with logs generated in go. Things are printed to stdout and a log file using io.multiwriterlog := io.MultiWriter(os.Stdout, logfile). I would like to add timestamps to both os.Stdout and logfile.

My approach is to write a write function

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...))
}
log := io.MultiWriter(&writer{os.Stdout, "2006/01/02 15:04:05"}, logFile)

However, it does not produce the timestamp both in Stdout and logfile. Does anyone know if there is another way to do this?

答案1

得分: 2

时间戳不显示在日志文件中的原因是因为只有你的写入器(writer.Writer)添加了时间戳,而不是io.MultiWriter()。将多写入器设置为writer.Writer,这样时间戳就会发送到两个写入器:os.StdoutlogFile

logFile := &bytes.Buffer{}
log := &writer{io.MultiWriter(os.Stdout, logFile), "2006/01/02 15:04:05"}
log.Write([]byte(" hi"))

fmt.Println("\nlogFile:", logFile)

这将输出(在Go Playground上尝试):

2009/11/10 23:00:00 hi
logFile: 2009/11/10 23:00:00 hi

还要注意,log包(与io.MultiWriter结合使用)可以直接提供此功能,您可以使用log.SetOutput()设置输出写入器:

logFile := &bytes.Buffer{}
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
log.Print("hi")

fmt.Println("logFile:", logFile)

输出结果(在Go Playground上尝试):

2009/11/10 23:00:00 hi
logFile: 2009/11/10 23:00:00 hi

此外,log包还提供格式化功能和可配置的输出。

英文:

The reason why the timestamp doesn't show up in the log file is because only your writer adds the timestamp, not io.MultiWriter().

Change your use to set the multi-writer to writer.Writer, so the timestamp will be sent to both writers: os.Stdout and logFile.

logFile := &bytes.Buffer{}
log := &writer{io.MultiWriter(os.Stdout, logFile), "2006/01/02 15:04:05"}
log.Write([]byte(" hi"))

fmt.Println("\nlogFile:", logFile)

This will output (try it on the Go Playground):

2009/11/10 23:00:00 hi
logFile: 2009/11/10 23:00:00 hi

Also note that the log package (combined with io.MultiWriter) gives you this functionality out of the box, you can set the output writer using log.SetOutput():

logFile := &bytes.Buffer{}
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
log.Print("hi")

fmt.Println("logFile:", logFile)

Output (try it on the Go Playground):

2009/11/10 23:00:00 hi
logFile: 2009/11/10 23:00:00 hi

Moreover, the log package also provides formatting capabilities and configurable output.

huangapple
  • 本文由 发表于 2021年8月3日 17:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/68633556.html
匿名

发表评论

匿名网友

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

确定