Logging to a file - rewrite
- Set the output filehandle using SetOutput
examples/logging-to-file/logging_to_file.go
package main import ( "log" "os" ) func main() { var filename = "logging_to_file.log" var fh, err = os.Create(filename) if err != nil { log.Fatalf("Could not open file '%v': %v", filename, err) } log.SetOutput(fh) log.Print("Hello logfile") log.Fatal("This is bad") }
examples/logging-to-file/logging_to_file.log
2020/04/10 08:55:04 Hello logfile 2020/04/10 08:55:04 This is bad