Much nicer logs
This commit is contained in:
parent
0fd1e44121
commit
bca1d2036a
4 changed files with 98 additions and 76 deletions
|
|
@ -1,7 +1,6 @@
|
|||
use crate::resources::Resource;
|
||||
use crate::to_artifact::ToArtifact;
|
||||
use slog::Drain;
|
||||
use slog::{OwnedKVList, Record};
|
||||
use slog::{Drain, Filter, OwnedKVList, Record};
|
||||
use slog_async::AsyncRecord;
|
||||
use std::cell::RefCell;
|
||||
use std::error::Error;
|
||||
|
|
@ -54,17 +53,17 @@ impl Drain for Recorder {
|
|||
}
|
||||
|
||||
impl Recorder {
|
||||
pub fn to_string(&self, filter_level: slog::Level) -> String {
|
||||
pub fn into_string(self, filter_level: slog::Level) -> String {
|
||||
let output = Output::new(vec![]);
|
||||
{
|
||||
let decorator = slog_term::PlainDecorator::new(output.clone());
|
||||
let drain = slog_term::CompactFormat::new(decorator).build();
|
||||
for record in &*self.0.lock().unwrap() {
|
||||
record.as_record_values(|record, kv| {
|
||||
if record.level() <= filter_level {
|
||||
drain.log(record, kv).unwrap();
|
||||
}
|
||||
});
|
||||
let drain = Filter::new(
|
||||
slog_term::CompactFormat::new(decorator).build(),
|
||||
move |record| record.level().is_at_least(filter_level),
|
||||
);
|
||||
let Ok(mutex) = Arc::try_unwrap(self.0) else { panic!("cannot unwrap Arc") }; // AsyncRecord does not implement Debug, so we cannot unwrap
|
||||
for record in mutex.into_inner().unwrap() {
|
||||
record.log_to(&drain).unwrap();
|
||||
}
|
||||
}
|
||||
String::from_utf8(Rc::try_unwrap(output.0).unwrap().into_inner())
|
||||
|
|
@ -80,6 +79,6 @@ mod test {
|
|||
#[test]
|
||||
fn records_no_output() {
|
||||
let recorder = Recorder::default();
|
||||
assert_eq!(recorder.to_string(Level::Trace), "");
|
||||
assert_eq!(recorder.into_string(Level::Trace), "");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue