rust-cli / env_logger

A logging implementation for `log` which is configured via an environment variable.
https://docs.rs/env_logger
Apache License 2.0
782 stars 124 forks source link

`Target::Pipe` does not auto-flush like std{out/err} #278

Closed miraclx closed 6 months ago

miraclx commented 10 months ago

By default, env_logger writes to stderr using termcolor which defers to the std::io::Write implementation of std::io::Stderr. That implementation auto-flushes the buffer when a new line is found.

For most cases, this works fine because log auto-appends a new line at the end of the record.

But when using a custom sink (via Target::Pipe), for example - when writing to a file, io::Write::write is called, but io::Write::flush is never called.

This assumes that the io::Write::write implementation of the sink will flush the buffer because stdout & stderr do. But they're outliers in this. Writing does not imply flushing.

This also means if for whatever reason the last record isn't newline terminated, that record will be lost. Buffered in memory but never written to the destination.