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

Filtering by the presence or absence of a key #19

Closed
seanpianka opened this issue Mar 11, 2021 · 0 comments
Closed

Filtering by the presence or absence of a key #19

seanpianka opened this issue Mar 11, 2021 · 0 comments

Comments

@seanpianka
Copy link

Based on discussion from #16, a new commit has added support for filtering by the absence or presence of keys in a record.

Here's a example filter which builds a filter that should match these semantics:

"Pass message if key_exists(key) && not(key_matches(key, bad_value)) && not(key_matches(key, ""))".

fn filter<D>(drain: D, level: Level) -> KVFilter<D> where D: Drain {
    KVFilter::new(drain, level)
        .only_pass_on_any_key_present(["err".to_string()].iter())
        .always_suppress_any(Some(
            HashMap::from_iter(
                vec![(
                    "err".to_string(),
                    HashSet::from_iter(vec!["None".to_string(), "".to_string()]),
                )]
            )
        ))
}

To verify the behavior, below is a unit test to verify the above semantics of the filter.

#[cfg(test)]
mod tests {
    use slog::{error, debug, info, Logger, Level, Drain};
    use super::filter;

    #[test]
    fn should_not_log_info_messages() {
        let decorator = slog_term::PlainSyncDecorator::new(slog_term::TestStdoutWriter);
        let drain = slog_term::FullFormat::new(decorator).build().fuse();
        let filter = filter(drain, Level::Debug).fuse();
        let logger = Logger::root_typed(filter, o!());

        // should discard
        info!(logger, "NO: test info");
        info!(logger, "NO: test info"; "count" => 10);
        info!(logger, "NO: test error"; "err" => "None");
        info!(logger, "NO: test error"; "err" => "");
        debug!(logger, "NO: test debug");

        // should log to drain
        info!(logger, "YES: test error"; "err" => "Panic!");
        error!(logger, "YES: test error");
    }
}

The output from this test shows several failure cases, i.e. a record that should be filtered was not.

Mar 10 18:58:03.032 INFO NO: test info
Mar 10 18:58:03.033 INFO NO: test info, count: 10
Mar 10 18:58:03.033 INFO NO: test error, err: None
Mar 10 18:58:03.033 INFO NO: test error, err: 
Mar 10 18:58:03.033 INFO YES: test error, err: Panic!
Mar 10 18:58:03.033 ERRO YES: test error

@przygienda I thought it'd be better to create a new issue for this, rather than inside an unrelated PR.

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

1 participant