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

log key-value pairs #1

Merged
merged 1 commit into from
Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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