Skip to content

Instantly share code, notes, and snippets.

@jwreagor
Created October 15, 2017 02:41
Show Gist options
  • Save jwreagor/bebfa1952a8fad4215616fd7a522f5db to your computer and use it in GitHub Desktop.
Save jwreagor/bebfa1952a8fad4215616fd7a522f5db to your computer and use it in GitHub Desktop.
if opts.LogFile != "" {
logFp, err := os.OpenFile(opts.LogFile, os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0600)
checkError(fmt.Sprintf("error opening %s", opts.LogFile), err)
defer logFp.Close()
// ensure panic output goes to log file
syscall.Dup2(int(logFp.Fd()), 1)
syscall.Dup2(int(logFp.Fd()), 2)
// log as JSON
log.SetFormatter(&log.JSONFormatter{})
// send output to file
log.SetOutput(logFp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment