Abnormal behavior of log4go

2019-07-15 09:12发布

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?

标签: logging go
4条回答
叼着烟拽天下
2楼-- · 2019-07-15 09:47

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

查看更多
Lonely孤独者°
3楼-- · 2019-07-15 09:53

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.

查看更多
够拽才男人
4楼-- · 2019-07-15 10:07

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.

查看更多
手持菜刀,她持情操
5楼-- · 2019-07-15 10:07

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.

查看更多
登录 后发表回答