简体   繁体   English

转到:文件错误记录中的新行

[英]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. 假设某处需要一个“ \\ n”,但我正在努力弄清楚。

Thanks 谢谢

It was a case of using Print with carriage return and line feed before the Fatal call: 这是在致命调用之前将Print与回车符和换行符一起使用的情况:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM