rust-lang/log

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

radix opened this issue · 5 comments

radix commented

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.

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

@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.

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

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);

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

log/Cargo.toml

Lines 51 to 54 in b834897

kv_unstable = ["value-bag"]
kv_unstable_sval = ["kv_unstable", "value-bag/sval", "sval", "sval_ref"]
kv_unstable_std = ["std", "kv_unstable", "value-bag/error"]
kv_unstable_serde = ["kv_unstable_std", "value-bag/serde", "serde"]

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.