Skip to content

Failing key lookup for key_name = value key insertion #731

Description

@ZornitsaAsanska

After the 0.4.31 update, record.key_values().get(Key::from("key_name")) fails to find keys that were stored via the log::warn!(key_name = value; "msg") macro. The macro now stores keys as Key::from_str_static (Static variant), but Key::from() creates a Borrowed variant, and the lookup doesn't match them despite identical string content.

Nvm: I guess using a Key::from_str_static in the look up would do it :D

use log::kv::{Key, Value, VisitSource};
use log::{Log, Metadata, Record};

struct TestLogger;

struct PrintVisitor;
impl<'a> VisitSource<'a> for PrintVisitor {
    fn visit_pair(&mut self, key: Key<'a>, value: Value<'a>) -> Result<(), log::kv::Error> {
        println!("  key: {:?}, value: {:?}", key, value);
        Ok(())
    }
}

impl Log for TestLogger {
    fn enabled(&self, _: &Metadata) -> bool { true }
    fn flush(&self) {}
    fn log(&self, record: &Record) {
        println!("=== Record ===");
        let _ = record.key_values().visit(&mut PrintVisitor);
        println!("key_values.get(Key::from(\"tags\")): {:?}", record.key_values().get(Key::from("tags")));
        println!("key_values.count(): {}", record.key_values().count());

        let tags = record.key_values().get(Key::from("tags"));
        assert!(tags.is_some(), "expected tags to be present, but get() returned None");
    }
}

static LOGGER: TestLogger = TestLogger;

fn main() {
    log::set_logger(&LOGGER).unwrap();
    log::set_max_level(log::LevelFilter::Trace);

    const TAG1: &str = "ConstantTag1";
    log::warn!(tags = TAG1; "test message");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions