I've been thinking of a project where I spawn multiple threads to monitor certain aspects of my machine. I was wondering what a good approach might be to synchronize logging across all my threads?
If my threads monitor on short timescale, I don't want any threads to block on writing to my log file, so I had the thought that I would have a dedicated thread for writing to the log file.
With that in mind, are channels the best way to approach this? E.g. multiple threads would all share the same Receiver (the log writing thread). Whenever a thread needs to write to a log, it can send the log output to the single Receiver. Then, the Receiver can write to the logfile while the monitoring threads can continue their execution.
If I understand correctly, I can use channel in std::sync::mpsc - Rust to create a single Receiver
and clone Sender
, where Receiver
would be my log writing thread, while the cloned Sender
would be the monitoring threads.
Am I thinking about this correctly? Are there better approaches to take?