slog icon indicating copy to clipboard operation
slog copied to clipboard

Logging a SerdeValue panics when the drain doesn't support nested-values

Open atroche opened this issue 4 years ago • 4 comments

Hello!

I'm deriving SerdeValue for a struct, and I've got the project building. But when I try to log it out, I get a panic looking like this:

https://gist.github.com/atroche/2db3b55ffa4eea524b2f7a5fbdb164c4

I've tried it with three different drains: slog_term::CompactFormat, slog::json::Json and slog_loggly::LogglyDrain, all to the same effect.

I've set up a project which is as close to a minimal failing example as I can get:

https://github.com/atroche/try_slog_serde_derive/blob/master/src/main.rs

This is the core of it:

#[derive(Clone, SerdeValue, Serialize)]
struct MyString(String);

fn main() {
    let drain = Mutex::new(slog_json::Json::default(std::io::stdout())).map(slog::Fuse);
    let logger = slog::Logger::root(drain, o!());
    let my_string = MyString("testing".into());
    debug!(logger, "Hello, world!"; "my_string" => my_string);
}

Am I doing something wrong?

Cheers!

atroche avatar Sep 17 '19 07:09 atroche

I've also found that I have no trouble serializing my_string with (e.g.) serde_json like this:

let my_string = MyString("testing".into());
let serde_value = my_string.as_serde();
let string = serde_json::to_string(serde_value).unwrap();

atroche avatar Sep 17 '19 07:09 atroche

Aha, so it seems Drains need to explicitly support nested-values, which slog-term doesn't but slog-json does, behind a feature flag. Bugger!

Is there a way to log a HashMap of strings without using SerdeValue (so that I can use drains not designed to handle nested-values)? E.g., if I implement Value directly for them?

atroche avatar Sep 17 '19 13:09 atroche

As far as I can see, SerdeValue is a panic!() by default, and is not implement for T: serde:Serialize. But SerdeValue is really a bit confusing, I have spend several hours on it and it still not working.....

ChaseElectr avatar Sep 17 '19 13:09 ChaseElectr

I wonder if there's a way to make your program not type check when you log a SerdeValue to a drain that doesn't support nested values. That definitely would've saved me some time! Alternatively, a descriptive runtime error would've help too, although that might be harder because the error is happening inside the drain and not slog itself.

atroche avatar Sep 17 '19 23:09 atroche