daboross / fern

Simple, efficient logging for Rust
MIT License
848 stars 51 forks source link

How to filter by `&Record` #81

Open broccolihighkicks opened 3 years ago

broccolihighkicks commented 3 years ago

Is it possible to filter based on the data in a &Record?

The normal .filter() fn only provides &log::Metadata which only contains a level and a str target.

Record contains information about the file path which I want to use to group by "my-app-workspace", "my-other-local-crates" and "cargo dep".

fern::Dispatch::new()
    .format(move |out, message, record| {
        if !include_in_output(record) {
            // Issue: Returning from `format` seems to default to printing `message`
            return;
        }

        out.finish(format_args!("{} {}", "a", "b"));
    });

Thanks

daboross commented 3 years ago

Right - there isn't, but this would definitely be useful to add. I'm not 100% sure why these aren't included in Metadata, since file and module_path do seem like metadata, but they aren't.

I don't think it will be possible to add this within the current format method, so I may need to add a new method and make the transfer over only on the next breaking release.