Skip to content

Instantly share code, notes, and snippets.

@CeoFred
Created January 4, 2023 16:41
Show Gist options
  • Save CeoFred/d597de551614581d4506f12c411d3f99 to your computer and use it in GitHub Desktop.
Save CeoFred/d597de551614581d4506f12c411d3f99 to your computer and use it in GitHub Desktop.
Log errors to a file in golang
package main
import "fmt"
import "os"
import "log"
func main() {
logfile,err := os.Create("log.txt")
if err != nil {
fmt.Println("failed to create log file: ", err)
}
log.SetOutput(logfile)
_, err = os.Open("no-file.txt")
if err != nil {
fmt.Println("err happened ", err)
log.Println(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment