Lint `non_structural_log`
Veetaha opened this issue · 0 comments
Lint explanation
Catch code that uses a tracing
logging macro without using structural logging. According to the conventions that I use it is prohibited to use format arguments in tracing
logging macros. This means that the message
of the log event must always be a static string, and no placeholder {}
syntax can be used.
The reasoning is that when we look at the logs in grafana/loki
we want to see a birds-eye view of what the system does without digging into the specifics of what specific values of different variables there were when the log happened like this:
and then expand the log event to see the key-value pairs of the structural log to dig into the details of a particular log in time.
If developers don't use structural logging and interpolate values in the log message, then such a birds-eye view becomes very verbose and hard to navigate. Also, when no string interpolation is used in log events it's possible to easily build the metrics and statistics of how often a log event with message "foo bar" occurs, and graph that on a dashboard.
Example code
Bad
tracing::info!("The user `{username}` joined the chat `{}`", chat.id);
Good
tracing::info!(%username, chat = %chat.id, "The user joined the chat");
Notes
It would be easier to implement such kind of a lint if marker
could see unexpanded macros, but I suppose it may be possible to craft a hacky version of this that operates on the expanded tracing
macros, although it may break at any moment once tracing
changes the way their macros are expanded.