gopherdata / gophernotes

The Go kernel for Jupyter notebooks and nteract.
MIT License
3.8k stars 264 forks source link

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

Open kesmit13 opened 5 years ago

kesmit13 commented 5 years ago

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})  
kesmit13 commented 5 years ago

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.

cosmos72 commented 5 years ago

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?