无法使用`’>’`或`’>>’`从`log.Println()`和`log.Printf()`中写入文件。

huangapple go评论70阅读模式
英文:

Can not use '>' or '>>' to write to file from log.Println() and log.Printf()

问题

我尝试使用>>来将内容写入文件,这在我的Go项目中通常是有效的,但现在却不起作用。

./main >> info.log 或者 go run main.go >> info.log

我以root身份运行,并尝试将文件权限更改为755,但在info.log中仍然没有任何内容。我还尝试了ls >> ls.log,它可以正常工作。
所以我认为我的代码有问题。我只使用了log.Println()log.Printf()

我的代码在Ubuntu 12.04.5 LTS (GNU/Linux 3.13.0-32-generic x86_64)上运行。

英文:

I try to write to file using >> as usual by for Go project, It does not work.

./main >> info.log or go run main.go >> info.log

I'm running as root and I've tried to change file permission to 755 but still noting in info.log. I've also tried ls >> ls.log It works fine.
So I think there is something wrong with my code. All I use are log.Println() and log.Printf()

My code runs on Ubuntu 12.04.5 LTS (GNU/Linux 3.13.0-32-generic x86_64)

答案1

得分: 8

Go的标准日志记录器写入stderr。你应该在你的shell脚本中使用2>2>>及其相关命令,或者创建自己的日志记录器来写入stdout。

英文:

Go's standard logger writes to stderr. You should either use 2> and 2>> and their friends in your shell scripts, or create your own logger that writes to stdout.

答案2

得分: 2

我不认为你正在写入标准输出(stdout),尝试使用fmt而不是log。如果默认的fmt.Printlnfmt.Printf不起作用,可以在这里找到有关该包的详细信息,可以获取一个特定于stdout或stderr的io.Writer,这肯定会起作用。文档在这里:https://golang.org/pkg/fmt/

英文:

I don't think you're writing to stdout, try using fmt instead of log. If the default fmt.Println and fmt.Printf don't work, the package details are here can get an io.Writer specific to stdout or stderr which will surely work. The docs are here https://golang.org/pkg/fmt/

huangapple
  • 本文由 发表于 2015年7月8日 00:45:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/31274669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定