gopherdata/gophernotes

zerolog: could not write event: write |1: file already closed

kesmit13 opened this issue · 2 comments

I'm using zerolog in my package and when I try to use it within gophernotes, I get the following error every time it is used:

zerolog: could not write event: write |1: file already closed

The logger itself is constructed using a ConsoleWriter that writes to os.Stderr. I'm guessing that gophernotes does something to swap out os.Stderr so that it writes to the notebook rather then the terminal where it was started, but I'm not sure how to account for this in my code.

zerolog.New(ConsoleWriter{Out: os.Stderr})  

It looks like zerolog must keep a reference to the temporary stream that gophernotes creates, so when it tries to write to it later, it's gone. I ended up wrapping os.Stderr with another writer that always does the os.Stderr lookup every Write so that it always picks up the current one.

I am glad you found a solution :)

Yet I think it's unfortunate that os.Stderr is replaced by a different io.Writer at each cell evaluation - it surprises users and forces them to write workarounds, as you had to.

I am thinking whether replacing os.Stderr only once at kernel startup is better - it would still have peculiar behavior, as it's not obvious what should be done if a goroutine writes to it while there's no cell evaluation in progress:

Should os.Stderr buffer the received text and show it at the first opportunity?
Should it silently discard the text?
Or should it return an error, as it does now?