-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): add tracing, verbosity, json output
- Loading branch information
Showing
19 changed files
with
423 additions
and
199 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,101 @@ | ||
#[macro_export] | ||
macro_rules! warnln { | ||
($($arg:tt)*) => { | ||
println!("{} {}", "[WARN]".color(Color::BrightYellow).bold(), format!($($arg)*)) | ||
}; | ||
} | ||
use tracing::{Event, Level, Subscriber}; | ||
use tracing_subscriber::{ | ||
fmt::{ | ||
self, | ||
format::{FmtSpan, Writer}, | ||
FmtContext, FormatEvent, FormatFields, | ||
}, | ||
registry::LookupSpan, | ||
}; | ||
|
||
#[macro_export] | ||
macro_rules! infoln { | ||
($($arg:tt)*) => { | ||
println!("{} {}", "[INFO]".color(Color::BrightBlue).bold(), format!($($arg)*)) | ||
}; | ||
} | ||
use crate::cli::Args; | ||
|
||
#[macro_export] | ||
macro_rules! errorln { | ||
($($arg:tt)*) => { | ||
eprintln!("{} {}", "[ERROR]".color(Color::BrightRed).bold(), format!($($arg)*)) | ||
}; | ||
} | ||
use super::color::{Color, ColorExt}; | ||
|
||
#[macro_export] | ||
macro_rules! successln { | ||
($($arg:tt)*) => { | ||
println!("{} {}", "[SUCCESS]".color(Color::BrightGreen).bold(), format!($($arg)*)) | ||
}; | ||
} | ||
pub struct CustomFormatter; | ||
|
||
#[macro_export] | ||
macro_rules! warn { | ||
($($arg:tt)*) => { | ||
print!("{} {}", "[WARN]".color(Color::BrightYellow).bold(), format!($($arg)*)) | ||
}; | ||
impl<S, N> FormatEvent<S, N> for CustomFormatter | ||
where | ||
S: Subscriber + for<'a> LookupSpan<'a>, | ||
N: for<'a> FormatFields<'a> + 'static, | ||
{ | ||
fn format_event( | ||
&self, | ||
ctx: &FmtContext<'_, S, N>, | ||
mut writer: Writer<'_>, | ||
event: &Event<'_>, | ||
) -> std::fmt::Result { | ||
match *event.metadata().level() { | ||
Level::TRACE => write!(writer, "{} ", "[TRACE]".color(Color::BrightMagenta)), | ||
Level::DEBUG => write!(writer, "{} ", "[DEBUG]".color(Color::BrightBlue)), | ||
Level::INFO => write!(writer, ""), | ||
Level::WARN => write!(writer, "{} ", "[WARN]".color(Color::BrightYellow)), | ||
Level::ERROR => write!(writer, "{} ", "[ERROR]".color(Color::BrightRed)), | ||
}?; | ||
|
||
ctx.field_format().format_fields(writer.by_ref(), event)?; | ||
|
||
writeln!(writer) | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! info { | ||
($($arg:tt)*) => { | ||
print!("{} {}", "[INFO]".color(Color::BrightBlue).bold(), format!($($arg)*)) | ||
}; | ||
pub struct CleanJsonFormatter; | ||
|
||
impl<S, N> FormatEvent<S, N> for CleanJsonFormatter | ||
where | ||
S: Subscriber + for<'a> LookupSpan<'a>, | ||
N: for<'a> FormatFields<'a> + 'static, | ||
{ | ||
fn format_event( | ||
&self, | ||
ctx: &FmtContext<'_, S, N>, | ||
mut writer: Writer<'_>, | ||
event: &Event<'_>, | ||
) -> std::fmt::Result { | ||
let mut fields_output = String::new(); | ||
|
||
let temp_writer = Writer::new(&mut fields_output); | ||
ctx.field_format().format_fields(temp_writer, event)?; | ||
let clean_fields = strip_ansi_escapes::strip_str(&fields_output); | ||
|
||
let json = serde_json::json!({ | ||
"level": event.metadata().level().to_string(), | ||
"fields": clean_fields, | ||
"target": event.metadata().target(), | ||
}); | ||
|
||
writeln!(writer, "{}", json) | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! error { | ||
($($arg:tt)*) => { | ||
eprint!("{} {}", "[ERROR]".color(Color::BrightRed).bold(), format!($($arg)*)) | ||
pub fn setup_logging(args: &Args) { | ||
let filter_level = if args.quiet { | ||
Level::ERROR | ||
} else if args.verbose >= 2 { | ||
Level::TRACE | ||
} else if args.verbose == 1 { | ||
Level::DEBUG | ||
} else { | ||
Level::INFO | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! success { | ||
($($arg:tt)*) => { | ||
print!("{} {}", "[SUCCESS]".color(Color::BrightGreen).bold(), format!($($arg)*)) | ||
let builder = fmt::Subscriber::builder() | ||
.with_env_filter(format!("soar_cli={}", filter_level)) | ||
.with_target(false) | ||
.with_thread_ids(false) | ||
.with_thread_names(false) | ||
.with_file(false) | ||
.with_line_number(false) | ||
.with_span_events(FmtSpan::NONE) | ||
.with_writer(std::io::stderr) | ||
.compact() | ||
.without_time(); | ||
|
||
let subscriber: Box<dyn Subscriber + Send + Sync> = if args.json { | ||
Box::new(builder.event_format(CleanJsonFormatter).finish()) | ||
} else { | ||
Box::new(builder.event_format(CustomFormatter).finish()) | ||
}; | ||
|
||
tracing::subscriber::set_global_default(subscriber).expect("Failed to set tracing subscriber"); | ||
} |
Oops, something went wrong.