slog-rs / slog

Structured, contextual, extensible, composable logging for Rust
https://slog.rs/
Apache License 2.0
1.57k stars 95 forks source link

Feature Request: thread_id #312

Closed breezewish closed 2 years ago

breezewish commented 2 years ago

It would be great if the thread_id could be printed out, which could be helpful to learn the context in multi-threaded applications.

Techcable commented 2 years ago

I believe this should be possible through the use of the FnValue struct.

The following code should create a new child logger. The new child will have a thread_id key, computed by the "FnValue". The FnValue will debug-display the ThreadId

let child = parent.new(o!(
    "thread_id" => slog::FnValue(|| {
        format!("{:?}", std::thread::current().id())
    })
));
// any use of the `child` logger will have `thread_id` as context.
play(&child);

You should also be able to modify this to work with the root loger (just move it to the initial o!() instead of creating a new child).

Please let me know if this doesn't work