rust-lang / log

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

"event" name support? (appending a suffix to the default target instead of replacing it entirely) #591

Closed radix closed 9 months ago

radix commented 9 months ago

The way I like to write log statements is such that each instance of a log message in my source code gets its own unique "event" name, distinct from the descriptive message associated with the log line. This allows me to quickly use log analysis tools to query/aggregate logs easily as long as I know the event name.

I can do this with the existing log macros by writing something like (in ptrpi/storage.rs):

debug!("ptrpi::storage::load-game", "Loading game file: {:?}", game_file);

However, writing out the ptrpi::storage part is something I would like to avoid -- I really just want the load-game name to be specified in each log message. Maybe something like:

debug!(event: "load-game", "Loading game file: {:?}", game_file);

Please let me know if I'm missing something and there's already an easy way to do this! I searched the issue tracker and couldn't find anything like this.

Thomasdezeeuw commented 9 months ago

You can use target for this. See the logging macros and Record.target. Example:

debug!(target: "load-game", "Loading game file: {:?}", game_file);

// In logging
let record: log::Record = // ..
println!("target: {}", record.target());

Note that it defaults to the module name.

Let me know if this does or doesn't work for you.

radix commented 9 months ago

@Thomasdezeeuw sorry, maybe I wasn't clear enough. The point of my post is that if I use target, and I want the module name to also be part of the target, I have to write ptrpi::storage::load-game, not just load-game.

Thomasdezeeuw commented 9 months ago

The point of my post is that if I use target, and I want the module name to also be part of the target, I have to write ptrpi::storage::load-game, not just load-game.

You could combine the module and target in you logging implementation.

Otherwise you can look at logging a separate key-value: https://docs.rs/log/latest/log/kv/index.html. It's officially still unstable, but it hasn't seen any breaking changes in months/years and it's close to stabilising (https://github.com/rust-lang/log/issues/4360).

radix commented 9 months ago

I think the kv log approach is probably my best bet, but I couldn't really figure out how to use it in the log crate. In the end I switched to tracing and am using this formulation:

debug!(event="load-snapshot", ?filename);
Thomasdezeeuw commented 9 months ago

I think the kv log approach is probably my best bet, but I couldn't really figure out how to use it in the log crate. In the end I switched to tracing and am using this formulation:

debug!(event="load-snapshot", ?filename);

For reference you need to enable the kv feature(s), i.e. kv_unstable and likely kv_unstable_std (unless you don't use std), see https://github.com/rust-lang/log/blob/b83489710240bb7b4154174534a92c8c44d4aa34/Cargo.toml#L51-L54

Then you can use the following:

log::debug!(event="load-snapshot", filename=log::as_debug!(filename));

When logging you can access event and filename in Record::key_values.

We're still discussing whether or not we want to support the ?filename notation as well.