Skip to content

Add env_logger to cli #197

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

Merged
merged 1 commit into from
Dec 18, 2018
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
2 changes: 2 additions & 0 deletions graphql_client_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
syn = "0.15"
log = "0.4.0"
env_logger = "0.6.0"

rustfmt-nightly = { version = "0.99" , optional = true }

Expand Down
42 changes: 42 additions & 0 deletions graphql_client_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
extern crate env_logger;
extern crate log;
use env_logger::fmt::{Color, Style, StyledValue};
use log::Level;

extern crate failure;
extern crate reqwest;
extern crate structopt;
Expand Down Expand Up @@ -70,6 +75,8 @@ enum Cli {
}

fn main() -> Result<(), failure::Error> {
set_env_logger();

let cli = Cli::from_args();
match cli {
Cli::IntrospectSchema {
Expand Down Expand Up @@ -100,3 +107,38 @@ fn main() -> Result<(), failure::Error> {
),
}
}

fn set_env_logger() {
use std::io::Write;

env_logger::Builder::from_default_env()
.format(|f, record| {
let mut style = f.style();
let level = colored_level(&mut style, record.level());
let mut style = f.style();
let file = style.set_bold(true).value("file");
let mut style = f.style();
let module = style.set_bold(true).value("module");
writeln!(
f,
"{} {}: {} {}: {}\n{}",
level,
file,
record.file().unwrap(),
module,
record.target(),
record.args()
)
})
.init();
}

fn colored_level<'a>(style: &'a mut Style, level: Level) -> StyledValue<'a, &'static str> {
match level {
Level::Trace => style.set_color(Color::Magenta).value("TRACE"),
Level::Debug => style.set_color(Color::Blue).value("DEBUG"),
Level::Info => style.set_color(Color::Green).value("INFO "),
Level::Warn => style.set_color(Color::Yellow).value("WARN "),
Level::Error => style.set_color(Color::Red).value("ERROR"),
}
}