Add key-values to the macros#461
Merged
Merged
Conversation
Attempt number two/three? Too many in any case.
Previously I proposed a design that followed a `struct` like syntax:
```rust
info!("my message: {}", arg, {
key1: "value1",
key2: 123,
});
```
However it turns out that this does not work well with named arguments
as reported in issues #369 and #372. The implementation was eventually
reverted in pr #374.
This new design takes inspiration from the `tracing` crate which already
supports key-value pairs in logging events. The basic idea is to put the
key-value pairs before the message and arguments. Applying the same
structure like syntax as above we would get something like the
following.
```rust
info!({
key1: "value1",
key2: 123,
}, "my message: {}", arg);
```
But personally I'm not a big fan of this formatting, let's try putting
everything on a single line instead.
```rust
info!({ key1: "value1", key2: 123 }, "my message: {}", arg);
```
A little better, but at this point the structure like syntax is really
more annoying then helpful. So, instead I've done away it, opting
instead use the following syntax.
```rust
info!(key1 = "value1", key2 = 123, "my message: {}", arg);
```
Two major differences:
* Removed the brackets.
* Colons (`:`) are replaced with equal/assignment signs (`=`).
This gives us syntax similar to variable assignment.
But then we run in some limitations of the macro syntax, specifically
that `expr` fragments aren't allowed after `expr` fragments. To fix this
I went with the easiest option of changing the last comma (`,`) after
the key-value pairs to a semicolon (`;`). Making the final syntax look
like the following.
```rust
info!(key1 = "value1", key2 = 123; "my message: {}", arg);
info!(target: "my_target", key1 = "value1", key2 = 123; "my message: {}", arg);
log!(target: "my_target", log::Level::Info, key1 = "value1", key2 = 123; "my message: {}", arg);
```
Which, in my opinion and all things considered, it's too bad looking.
Collaborator
Author
|
Ping @KodrAus. |
Collaborator
Author
|
@sfackler you have the time to look at this? |
Contributor
|
Hey @Thomasdezeeuw! 👋 Sorry I hadn't gotten to this. My GitHub notifications have been a mess lately. I don't currently have commit access to We are a bit limited in what we can do with This looks good to me 👍 |
KodrAus
approved these changes
Oct 26, 2021
EFanZh
pushed a commit
to EFanZh/log
that referenced
this pull request
Jul 23, 2023
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Attempt number two/three? Too many in any case.
Previously I proposed a design that followed a
structlike syntax:However it turns out that this does not work well with named arguments
as reported in issues #369 and #372. The implementation was eventually
reverted in pr #374.
This new design takes inspiration from the
tracingcrate which alreadysupports key-value pairs in logging events. The basic idea is to put the
key-value pairs before the message and arguments. Applying the same
structure like syntax as above we would get something like the
following.
But personally I'm not a big fan of this formatting, let's try putting
everything on a single line instead.
A little better, but at this point the structure like syntax is really
more annoying then helpful. So, instead I've done away it, opting
instead use the following syntax.
Two major differences:
:) are replaced with equal/assignment signs (=).This gives us syntax similar to variable assignment.
But then we run in some limitations of the macro syntax, specifically
that
exprfragments aren't allowed afterexprfragments. To fix thisI went with the easiest option of changing the last comma (
,) afterthe key-value pairs to a semicolon (
;). Making the final syntax looklike the following.
Which, in my opinion and all things considered, it's too bad looking.
/cc @KodrAus @yoshuawuyts