Skip to content

Commit

Permalink
log key-value pairs
Browse files Browse the repository at this point in the history
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
  • Loading branch information
yoshuawuyts authored and lrlna committed Jul 20, 2019
1 parent ea7403a commit 2de44e7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
29 changes: 27 additions & 2 deletions src/ndjson.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Print logs as ndjson.
use log::{LevelFilter, Log, Metadata, Record};
use log::{kv, LevelFilter, Log, Metadata, Record};
use serde_json::Value;
use std::collections::HashMap;
use std::time;
Expand Down Expand Up @@ -49,7 +49,7 @@ impl Log for Logger {
fn print_ndjson(record: &Record<'_>) {
let msg = Msg {
level: get_level(record.level()),
key_values: None,
key_values: format_kv_pairs(&record),
time: time::UNIX_EPOCH.elapsed().unwrap().as_millis(),
msg: record.args().to_string(),
};
Expand All @@ -66,3 +66,28 @@ fn get_level(level: log::Level) -> u8 {
Error => 50,
}
}

fn format_kv_pairs(record: &Record) -> Option<HashMap<String, Value>> {
struct Visitor {
key_values: Option<HashMap<String, Value>>,
}

impl<'kvs> kv::Visitor<'kvs> for Visitor {
fn visit_pair(
&mut self,
key: kv::Key<'kvs>,
val: kv::Value<'kvs>,
) -> Result<(), kv::Error> {
if let None = self.key_values {
self.key_values = Some(HashMap::new());
}
let kv = self.key_values.as_mut().unwrap();
kv.insert(key.to_string(), val.to_string().into());
Ok(())
}
}

let mut visitor = Visitor { key_values: None };
record.key_values().visit(&mut visitor).unwrap();
visitor.key_values
}
35 changes: 29 additions & 6 deletions src/pretty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Pretty print logs.
use console::style;
use log::{Level, LevelFilter, Log, Metadata, Record};
use log::{kv, Level, LevelFilter, Log, Metadata, Record};

#[derive(Debug)]
pub struct Logger {}
Expand Down Expand Up @@ -34,14 +34,37 @@ impl Log for Logger {
fn flush(&self) {}
}

// TODO: use format_key_val to pretty print kv's from log crate
fn pretty_print(record: &Record<'_>) {
println!("{}{}", format_message(&record), format_line(&record));
println!(
"{}{}{}",
format_message(&record),
format_line(&record),
format_kv_pairs(&record),
);
}

// TODO: use with key values that could eventually be put through log macros
fn _format_key_val(key: &str, val: &str) -> String {
format!(" › {}: {}\n", style(key).magenta(), val)
fn format_kv_pairs(record: &Record) -> String {
struct Visitor {
string: String,
}

impl<'kvs> kv::Visitor<'kvs> for Visitor {
fn visit_pair(
&mut self,
key: kv::Key<'kvs>,
val: kv::Value<'kvs>,
) -> Result<(), kv::Error> {
let string = &format!(" › {}: {}\n", style(key).magenta(), val);
self.string.push_str(string);
Ok(())
}
}

let mut visitor = Visitor {
string: String::new(),
};
record.key_values().visit(&mut visitor).unwrap();
visitor.string
}

fn format_line(record: &Record<'_>) -> String {
Expand Down

0 comments on commit 2de44e7

Please sign in to comment.