Derrick Zhang
Derrick Zhang

Reputation: 21501

Abnormal behavior of 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?

Upvotes: 2

Views: 1271

Answers (4)

ccpaging
ccpaging

Reputation: 1

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

Upvotes: 0

Derrick Zhang
Derrick Zhang

Reputation: 21501

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.

Upvotes: 2

kr1
kr1

Reputation: 7485

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.

Upvotes: 3

zzzz
zzzz

Reputation: 91193

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.

Upvotes: 1

Related Questions