Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smarter macros to allow writing the log message at the beginning #3080

Open
JasonXJ opened this issue Sep 12, 2024 · 0 comments
Open

Smarter macros to allow writing the log message at the beginning #3080

JasonXJ opened this issue Sep 12, 2024 · 0 comments

Comments

@JasonXJ
Copy link

JasonXJ commented Sep 12, 2024

Feature Request

Crates

tracing

Motivation

It is more natural to put the log message at the beginning. For example, this

info!("downloading sub-sitemap", url = sitemap.loc);

is better than

info!(url = sitemap.loc, "downloading sub-sitemap");

We can only do the latter currently. I believe this is to avoid ambiguity of whether url = sitemap.loc is a formatting argument or a structured field for the log. But I think there is a solution to this.

Proposal

We should allow putting message at the beginning. And if the message requires any formatting, it should be surrounded by [ ] (I pick these over other parentheses because it looks better to me).

Example of simple string without any formatting as the message (same example as above):

info!("downloading sub-sitemap", url = sitemap.loc);

Example of format string as the message:

info!(["downloading sub-sitemap for {domain}", domain = "example.com"], url = sitemap.loc);

To be backward compatible and compatible with the log crate, we will still support something like

info!(... "downloading sub-sitemap. domain={domain}", domain = "example.com")

This can be done by checking whether the message not wrapped in [ ] is actually a format string (i.e. contains un-escaped { }). And if it is, we would consider the following arguments to be formatting arguments just like what we are doing right now.

I feel that this is not too hard to implement, although we might need to switch to procedure macros to make it work.

Also, this is a bit off-topic, but I wonder why we have so much redundant code in macros.rs. Surely, there is a way to avoid having almost identical 250+ lines of implementation for info!(), debug!() ... Is this because of some limitation of declarative macros?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant