phuslu / log

Fastest structured logging
MIT License
672 stars 44 forks source link

MultiWriter panic with AsyncWriter #23

Closed xenking closed 2 years ago

xenking commented 3 years ago

Description: Got panic when trying to print messages on Logger with MultiWriter: AsyncWriter in ErrorWriter, InfoWriter, WarnWriter struct fields, and ConsoleWriter. Reproduction example: https://play.golang.org/p/gkNrfO2M0Jv

phuslu commented 3 years ago

Thanks for figure out, after initial investigation, I think it's a design bug(or miss usage?)

  1. async writer will recycle the log entry (return to memory pool) after it was written.
  2. so when multi writer try to write a recycled entry to console writer, panic happened.

The workaround is that dont use async writer as a underlying writer, it should use as a top writer.

I know this suggestion/limitation is not very make sense, so I call it may be "design bug", I'm considering a better way to implement async writer

  1. fix this problem
  2. dont hit performance
xenking commented 3 years ago

Tried to fix without performance downgrades. Can you check?

phuslu commented 3 years ago

I afraid that this fix will not 0-allocs for aysnc writer common cases, e.g. https://github.com/phuslu/log#asyncwriter