spdlog-rs icon indicating copy to clipboard operation
spdlog-rs copied to clipboard

[Feature Request] Structured logging (KV) support

Open SpriteOvO opened this issue 1 year ago • 3 comments

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::Value issues
    • 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.

SpriteOvO avatar Jul 25 '24 13:07 SpriteOvO

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 enum like serde_json::Value ourselves

    I 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::any

    Consider 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-bag

    Its 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.

SpriteOvO avatar Nov 19 '24 19:11 SpriteOvO

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 avatar Nov 20 '24 21:11 AlexTMjugador

@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.

SpriteOvO avatar Nov 23 '24 01:11 SpriteOvO

Completed in #77.

SpriteOvO avatar Sep 23 '25 13:09 SpriteOvO