Channel protocol or mutexes?

I’d like to write a logger trait that will log a specific set of events (e.g. create job, log progress, get jobs). Implementations will log to stdout / some file / postgres, etc.

My question is how best to implement multiple threads submitting logging requests. An mpsc::channel seems like the right way, but since there are multiple event types (with different payloads), I’d end up either with a receiver for each message type, or implementing a protocol over single channel.

The other options seems to be to use mutexes / Arc’s and functions defined on the trait for each event type.

Is there another approach that would be more idiomatic?

Sounds like an enum?

Yes, that’s what I was thinking. I would like the loggers to be able to provide some data (e.g. recent events) as well, so mutexes might be unavoidable.

Use crossbeam instead of std::mpsc. It's faster and more flexible in every aspect.

For logging, consider slog and tracing.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.