Allow macros to be more consistent with kvs #476
No reviewers
Labels
No labels
bug
duplicate
easy
enhancement
help wanted
invalid
question
rfc
structured-logging
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-rust-lang/log!476
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/kv-macros"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Part of #436
This tweaks the new key-value macro syntax slightly so that it can be more consistent in cases where key-values are supplied and in cases where they're not. All the old syntax is still supported, but we can now write
log!macros like:which then supports key-value pairs as:
For key-values, the
target = x;form is required. It splits our macro up into 3 logical parts, separated by;s:target = "my_target": The portion of the log record that's considered by top-level filtering. It's equivalent tolog_enabled!.key1 = value1, key2 = value2: The structured data associated with the log record."format {formatted1}", formatted1 = value1: The unstructured message associated with the log record.I think for
loghere we should also remove thecapture_*methods onValuefor now, because they lead to unpleasant compilation errors when you don't have a&T: 'staticaround. They're only useful if you want to try downcast to a specific type, like aUuidorTimestamp.I'll try flesh out our test coverage even more, since there are a lot of possible permutations of the log macros and their interactions with
format_argsto consider.cc @Thomasdezeeuw. What do you think?
Did
target: xnot work? That is what is currently used.And replace them with the
from_display!macros?It does work, but when I started using it I found the inconsistency with key values and named format args a bit jarring. We’ll probably want some
err = exprsyntax too at some point so thought starting to try normalize everything would be good.Those methods and direct downcasting will just go away, unless it’s something you’re currently using? Speaking of, what do you think of those little macros? The alternative is something like
tracing’s flags, which are similar toformat_args.I'm not sure. One the one hand I can see your argument, on the other target it is different from the key-values as it has its own method to access it (not via
Record::key_values, but viaRecord::target). Being inconsistent with the non-kvs macros could be annoying. For example when you quickly want to add a kv pair to a message you now also have to consider the target. But I don't know how munch of an annoyance that will be.I do use the downcasting but I'll switch to the visitor API introduces in https://github.com/rust-lang/log/pull/467, so a nice deprecation period would be appreciated.
I could either way. The way the tracing crate does is a bit unusual on first sight, but I image you get used to it. So it's a question of being more explicit (macros) vs quicker to write (tracing like flags). I'm with either.
Ah that's right, you've been forced to use a nasty chain of
if lets haven't you because that visitor API wasn't available. I'll make sure it doesn't disappear on you in the meantime.You can still specify key-value pairs without having to specify a target:
...but that does raise an interesting forwards compatibility hazard. Say you write:
In this scheme, that
targetwill end up as a key-value pair instead of as thetarget. Since we then default the target tomodule_path!()you'll end up withrecord.target()returning something else. This is solvable with a bit more macro work.Yeh, I think once you learn the scheme it's not too bad. My main reason not for trying to pursue it here is that I think formatting flags are a bit of a dead end. They're crafted for the needs of
format_args, but once we're outside their use-case we need to pick new arbitrary characters for new things, like capturing withserde, and those new things don't necessarily integrate with any other flags (what would it mean to capture withserdeand also pad with spaces?). But if we want to come up with a nice shorthand scheme we could deprecate theseas_*macros pretty easily.I don't want to keep you blocked on this for much longer though, so maybe after we're sure we're not painted into any corners (which I don't think we are this time) we can create a bit of a checklist on #343 of incomplete parts in the macros so they don't need to hold up getting something out?
I meant going from a log with a target to a log with a target and key-values, for example:
Or am I mistaken here?
I was saying it could be confusing. With your example above it's not clear that target is "special" (in the sense it can be retrieved using
Record.targetinstead of being another key-value pair). Keeping the ":" syntax might make it clearer?And it allows people to use
targetas a key in a key-value pair.👍
I think the only decision we need to make is for taget ":" vs "=". We can use macros for formatting and add special tracing-like flags later without breaking compatibility right?
Sounds good 👍
I’m not too strongly tied to my
=preference, possibly because I just don’t ever settargetmyself when logging. We could accept both forms still if we wanted to. I was thinking of working towards unifying key values with other pieces of metadata we associate with a log record so there isn’t a distinction in the end between target and anything else.I would be ok with keeping target using
:and deprecate it by making a key-value pair calledtargetjust work in the future.Sounds good. I think for backwards compatibility we want to keep
:for now.Ok, I've reverted the need to make use the
target =form in kv-enabled macros to avoid churning things. We can now technically write this though:In this case we'll interpret
targetas a key-value pair. This is how I think we'll be able to get away fromtarget:in the future, by making the macro effectively:where we can use a tt-muncher to unpack the key-value pairs and pick off a
target,erroretc if we find them, but don't have any special syntax for declaring them apart from any other piece of data you might want to associate with your record.Once this is green I'll merge it in and give it a smoke test in any candidate libraries to make sure it doesn't regress things and then get it out the door.
Nit: is using target here maybe confusing for new users?
This needs to be reverted to
:to fix the CI.