spdlog-rs
spdlog-rs copied to clipboard
[Feature Request] Structured logging (KV) support
Structured logging enhances traditional text-based log records with user-defined attributes. Structured logs can be analyzed using a variety of data processing techniques, without needing to find and parse attributes from unstructured text first.
With JsonFormatter (PR #69) added, the KV can be injected into the output logs as a separated JSON object for parsing by third-party programs.
Implementation
Working in PR #77.
Details
There are multiple possible implementations for kv::Value: enum (like serde_json::Value), std::any, log::kv::Value (based on crate value-bag), which one is better?
log:kv::Valueissues- rust-lang/log#641
Reference
A reference is the log kv implementation, we can see if we can improve anything base on that, and tracing's fields (thanks @AlexTMjugador).
There are more references in rust-lang/log#343.
I've started this work today, and trying the first step - implementing kv::Value. Basically I want to make sure that a kv::Value is downcastable and stored at almost zero cost.
There are multiple possible implementations, I'll try to summarize and keep updating the pros and cons of them here.
-
Make a
enumlikeserde_json::ValueourselvesI prefer not to do this because it requires a lot of code to be written and a lot of effort to keep them interactive.
-
Use
std::anyConsider how to support it for user-defined types? And if we need to support external traits (like
serde) we need to write some code for that as well. -
Use
value-bagIts implementation is a bit like
enum+any, and external traits are available out of the box. This seems pretty cool. The only drawback is that it comes from an external crate, should we wrap it or just expose it to the user?
I'm going to try value-bag first.
Hello! I just happened to notice that the issue I reported about log's KV downcasting was mentioned here, and I figured that I could give my two cents here: the more feature-rich tracing crate also supports structured logging, allowing events (basically, log records) to include typed fields identified by name. This might be worth considering as a reference for implementation as well.
@AlexTMjugador Hi, I'm happy to know that you saw this issue. :smile: Yes, tracing has a large user base and is practically tested, so I'm learning about its implementation of kv as well. Thank you for the information.
Completed in #77.