Drakulix / simplelog.rs

Simple Logging Facility for Rust
https://docs.rs/simplelog/
Apache License 2.0
414 stars 71 forks source link

Question: is it safe to use `TermLogger` across threads without additional synchronisation? #146

Closed cyqsimon closed 6 months ago

cyqsimon commented 6 months ago

After I call TermLogger::init at the start of the main thread then spin up multiple worker threads, is it safe to use the usual logging macros in these threads without additional synchronisation?

In other words, is it possible for two threads to print at the same time, and instead of producing two good lines, produce two interleaved lines of garbage? (I suspect no but just want confirmation.)

And lastly, what about SimpleLogger and WriteLogger? Do they behave the same?

Thanks in advance.

Drakulix commented 6 months ago

Yes, all loggers you mentioned to have an internal lock that prevents log messages from that same logger to interleave.

That said if you initialize multiple loggers in parallel (instead of using init to set a single global one) you can get interleaved lines, if they write to the same file (e.g. stdout).

cyqsimon commented 6 months ago

That's good to know. I've got no more questions.

Thank you again for the swift response.