Writing logs to multiple outputs in Go

The io.Writer interface is one of the most important interfaces in Go. We can take advantage of it to write logs to multiple places at the same time using the io.MultiWriter function.

For example, writing a log to the Standard Output (os.Stdout and a file os.File) is pretty straightforward.

f, _ := os.Create("log.txt")
defer f.Close()

// our writer
w := io.MultiWriter(os.Stdout, f)
logger := log.New(w, "MultiWriter loggger", log.LstdFlags)