1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use tracing_log::AsTrace;
pub struct LogTracer {
ignore: Vec<String>,
log: tracing_log::LogTracer,
}
impl LogTracer {
pub fn new<I: Into<String>>(ignore: impl IntoIterator<Item = I>) -> Self {
let ignore = ignore.into_iter().map(|x| x.into()).collect();
let log = tracing_log::LogTracer::default();
Self { ignore, log }
}
}
impl log::Log for LogTracer {
fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
// First, check the log record against the current max level enabled by
// the current `tracing` subscriber.
if metadata.level().as_trace() > tracing::level_filters::LevelFilter::current() {
// If the log record's level is above that, disable it.
return false;
}
// Okay, it wasn't disabled by the max level — do we have any specific
// modules to ignore?
if !self.ignore.is_empty() {
// If we are ignoring certain module paths, ensure that the metadata
// does not start with one of those paths.
let target = metadata.target();
for ignored in &self.ignore[..] {
if target.starts_with(ignored) {
return metadata.level() < log::Level::Debug;
}
}
}
// Finally, check if the current `tracing` dispatcher cares about this.
tracing::dispatcher::get_default(|dispatch| dispatch.enabled(&metadata.as_trace()))
}
fn log(&self, record: &log::Record<'_>) {
// those funny `log` crate idiots don’t actually call `enabled` before logging ...
if self.enabled(record.metadata()) {
// we can’t log the record ourselves because there is some recursive magic between
// tracing-subscriber and tracing-log that makes logging the right target work
self.log.log(record);
}
}
fn flush(&self) {}
}