rust-lang / log

Logging implementation for Rust
https://docs.rs/log
Apache License 2.0
2.12k stars 248 forks source link

Is this possible to make a failable log facade ? #588

Closed prx0 closed 6 months ago

prx0 commented 10 months ago

Hi, I'm wondering about why the log function from the Log trait does not return a Result<T, E> ? https://docs.rs/log/0.4.20/log/trait.Log.html#tymethod.log

The current signature is:

    fn log(&self, record: &Record);

If I cannot guarantee to write a log, should I panic instead of returning a Result::Err ? Or should I keep the error and do nothing here.

I see the authors of systemd-journald-loggers decided to panic in case of error. https://github.com/swsnr/systemd-journal-logger.rs/blob/main/src/lib.rs#L388

Is it the recommanded way to do ?

Thank you for reading

Thomasdezeeuw commented 10 months ago

I don't think making the log function failable is a good idea. Then you have error handling all over the place.

As for the what do on error, it's depends (when doesn't it, right?). You can try logging using an alternative method, for example fall back to logging to standard error. If that doesn't work, it depends if it's critical for you to keep your logs.

For my own logger, which already logs to standard error, I also panic when I fail to log.

Thomasdezeeuw commented 6 months ago

Closing as not planned.