James Milner
James Milner

Reputation: 899

Go: new line in file error logging

I am relatively new to go and looking to log out errors to a text file. At the moment I use:

// Logging
f, err := os.OpenFile("pgdump_errorlog.txt", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
    log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
//... (later on)
if err != nil {
  log.Fatal(err)
}

Which works OK, minus the fact that the errors do not produce a new line and just append to the end of the first line. Is there a way to make the error output create a new line before appending? I've tried:

if err != nil {
     log.Fatalf("\n Error: %v", err)
}

But this didn't log at all. Assuming there needs to be a "\n" somewhere but I am struggling to figure it out.

Thanks

Upvotes: 1

Views: 6914

Answers (1)

James Milner
James Milner

Reputation: 899

It was a case of using Print with carriage return and line feed before the Fatal call:

log.Print("\r\n")
//or log.Print(err.Error() + "\r\n")

Upvotes: 2

Related Questions