Skip to content
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 a SerdeValue panics when the drain doesn't support nested-values #232

Open
atroche opened this issue Sep 17, 2019 · 4 comments
Open

Comments

@atroche
Copy link

atroche commented Sep 17, 2019

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
Copy link
Author

atroche commented Sep 17, 2019

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
Copy link
Author

atroche commented Sep 17, 2019

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?

@ChaseElectr
Copy link

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

@atroche atroche changed the title Logging a SerdeValue panics Logging a SerdeValue panics when the drain doesn't support nested-values Sep 17, 2019
@atroche
Copy link
Author

atroche commented Sep 17, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants