log4go的异常行为

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

Abnormal behavior of log4go

问题

我发现 log4go 包有时会丢失日志。

以下是一个简单的代码片段(我移动了 log4go 目录,所以下面的导入是正确的):

package main
import (
    "log4go"
    "log"
    "fmt"
)

func main() {
    fmt.Println("fmt")
    log.Println("log")
    log4go.Info("log4go")
    log4go.Info("log4go")
}

然后我通过 go run test.go 执行它,输出如下:

fmt
2013/01/10 15:24:04 log

log4go 的消息没有写入输出。

为什么?

英文:

I found the log4go package loses logs from time to time.

Following is a simple code snippet(I moved the log4go directory so the following import is ok.):

package main
import (
    "log4go"
    "log"
    "fmt"
)

func main() {
    fmt.Println("fmt")
    log.Println("log")
    log4go.Info("log4go")
    log4go.Info("log4go")
}

Then I executed it by go run test.go, and the output is as follows:

fmt
2013/01/10 15:24:04 log

The messages by log4go are not written to output.

Why?

答案1

得分: 3

编辑:看起来他们的入门页面已经不再更新了,事实上,我在使用log4go时遇到了一些问题,无法将日志打印到标准输出。在版本3.0.1的文档中,他们指出:

  • 使用说明:- ConsoleLogWriter不会将消息的来源显示到标准输出,但FileLogWriter会。

在我的电脑上无法重现这个问题。唯一的解决方法是手动刷新(正如@jnml建议的那样),在记录日志后调用os.Stdout.Sync()。

总的来说,我觉得log4go的文档最近没有进行维护,示例代码无法正常工作,使用了已弃用的方法,因此整体行为很难理解。

英文:

edit: it seems their getting started page is no more up to date, in fact I had problems getting log4go to print to stdout at all, in the docs for version 3.0.1 they state:

  • Usage notes: - The ConsoleLogWriter does not display the source of the message to standard output, but the FileLogWriter does.

That was not reproducible on my box. the only way to have it print to stdout was to flush manually (as @jnml suggested) by calling os.Stdout.Sync() after logging calls.

generally my impression is that the docs of log4go are not maintained lately, the examples are not working, they use deprecated methods and the general behaviour is therefore hard to understand.

答案2

得分: 2

在检查log4go网站上的问题后,似乎log4go存在刷新问题。

因为它使用一个channel来写入文件,如果main退出得太快,日志内容将不会被写入。

因此,在代码片段的末尾添加time.Sleep(time.Second)将导致日志内容被刷新。

英文:

After checking the issues on log4go website, it seems that the log4go has a flushing problem.

Becuase it uses a channel to write to files, if the main exits too fast, the log content will not be written.

So adding a time.Sleep(time.Second) to the end of the code snippeet will cause the log content flush.

答案3

得分: 1

没有提供至少一个指向“log4go”的链接,很难回答这个问题(因此-1),所以让我猜一下:可能在某个地方缺少了对“flush”的调用。也许最好向包的作者报告这个问题?

顺便说一句:考虑到标准/推荐的Go设置,导入路径“log4go”是错误的。

英文:

Without even providing at least a link to "log4go" it's not easy to answer this question (hence -1) so let me just guess: There might be a missing call to 'flush' somewhere. Perhaps better report the issue to the package author(s)?

BTW: Also the import path "log4go" is broken, considering the standard/recommended Go setup.

答案4

得分: 0

简单地在Close()函数中添加一些代码,如下所示。

for i := 10; i > 0 && len(w.rec) > 0; i-- {
	time.Sleep(100 * time.Millisecond)
}

w.rec中有一些未保存的记录。

https://github.com/ccpaging/log4go

英文:

Simple add some code to Close() like below.

for i := 10; i > 0 && len(w.rec) > 0; i-- {
	time.Sleep(100 * time.Millisecond)
}

There some rec in w.rec not be saved.

https://github.com/ccpaging/log4go

huangapple
  • 本文由 发表于 2013年1月10日 15:26:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/14252766.html
匿名

发表评论

匿名网友

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

确定