Skip to content

Instantly share code, notes, and snippets.

@justinschuldt
Created November 22, 2021 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinschuldt/67d38959c6b9f79061858e88c3f45d4a to your computer and use it in GitHub Desktop.
Save justinschuldt/67d38959c6b9f79061858e88c3f45d4a to your computer and use it in GitHub Desktop.
Golang log to file
package main
import (
"os"
"time"
)
func main() {
logFileName := fmt.Sprintf("logs_%v.log", time.Now().String())
logFilePath := "./" + logFileName
f, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
wrt := io.MultiWriter(os.Stdout, f)
log.SetOutput(wrt)
log.Println("this goes to the conosle, and to the file.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment