-
Notifications
You must be signed in to change notification settings - Fork 741
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
Logging structs and vectors of structs via JSON subscriber (and Valuable) ... is broken? #3148
Comments
Just in case someone else runs into this, workaround (for myself) for the time being: use std::io::Error;
use serde::Serialize;
use serde_json::to_string;
use chrono;
#[derive(Serialize)]
struct Log<T> {
timestamp: String,
level: String,
fields: T,
target: String,
}
fn log<T: Serialize>(level: String, data: T) -> Result<(), Error> {
let log: Log<T> = Log {
timestamp: chrono::offset::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Micros, true),
level: level,
fields: data,
target: "tool::log".to_string(),
};
println!("{}", to_string(&log)?); // JSON
Ok(())
}
fn info_<T: Serialize>(data: T) -> Result<(), Error> {
log("INFO".to_string(), data)?;
Ok(())
}
#[derive(Serialize)]
struct Msg1<'a> {
msg: &'a Foo,
}
#[derive(Serialize)]
struct Msg2<'a> {
msg: &'a Vec<Foo>,
} ... which allows me to do ... info_(Msg1 { msg: &stuff })?;
info_(Msg2 { msg: &otherstuff })?; ... resulting in the desired output and otherwise, well, generating output that fits nicely into what |
What you tried to do in the first place is correct and was supported until recently. I have just run a Based on the time of your message, it seems that it has been broken for at least the last two releases. Thank you for the workaround, but for me going without Valuable and have to refactor this on an entire code base will be a no go. |
After digging a bit, it looks like in my case, i was also pulling After updating everything to the same version to align with what's coming with Solution: verify your dependencies on |
Bug Report
Version
Platform
Linux e2 5.15.0-124-generic #134-Ubuntu SMP Fri Sep 27 20:20:17 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
rustc 1.81.0 (eeb90cda1 2024-09-04)
Crates
Description
I tried to make sense of this and this in the context of logging JSON, which I could not really make work. Minimal example:
The JSON output is not really valid JSON.
Am I assuming correctly that, after reading #1570 and this comment on it, what I am attempting to do is not fully supported (yet)? Or am I missing something?
The text was updated successfully, but these errors were encountered: