Source

go-wise / multi-log.go

/* Example how to log to multiple destinations */
package main

import (
	"io"
	"log"
	"os"
)

/* Init function, called by the go runtime */
func init() {
	/* Log file */
	filename := "/tmp/log.out"
	flags := os.O_WRONLY|os.O_APPEND|os.O_CREATE

	if file, err := os.OpenFile(filename, flags, 0666); err != nil {
		// This will go only to os.Stderr
		log.Fatalf("error: can't open %s (%v)", filename, err)
	} else {
		log.SetOutput(io.MultiWriter(os.Stdout, file))
	}
}

func main() {
	log.Print("I'm am multiplexed") // Will go to screen and log file
}