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?