-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
169 additions
and
11 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
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
use std::collections::HashMap; | ||
|
||
use itertools::Itertools; | ||
use nu_ansi_term::{Color, Style}; | ||
use tracing::{field::Field, Level}; | ||
use tracing_subscriber::Layer; | ||
|
||
pub struct CraftLayer; | ||
|
||
impl<S: tracing::Subscriber> Layer<S> for CraftLayer { | ||
fn on_event( | ||
&self, | ||
event: &tracing::Event<'_>, | ||
_context: tracing_subscriber::layer::Context<'_, S>, | ||
) { | ||
// inspiration taken from pnpm | ||
let level = Color::Black | ||
.on(match *event.metadata().level() { | ||
Level::INFO => Color::Green, | ||
Level::WARN => Color::Yellow, | ||
Level::ERROR => Color::Red, | ||
Level::DEBUG => Color::Blue, | ||
Level::TRACE => Color::Purple, | ||
}) | ||
.bold() | ||
.paint(format!(" {} ", event.metadata().level().as_str())); | ||
|
||
let pad_level = " ".repeat(5 - event.metadata().level().as_str().len()); | ||
|
||
let mut visitor = CraftVisitor { | ||
message: None, | ||
other_fields: HashMap::new(), | ||
}; | ||
|
||
event.record(&mut visitor); | ||
|
||
let message = visitor | ||
.message | ||
.as_deref() | ||
.map(|message| format!(" {message}")) | ||
.unwrap_or(String::new()); | ||
|
||
// should look like | ||
// field = value field2 = value2 | ||
let other_fields = if visitor.other_fields.is_empty() { | ||
String::new() | ||
} else { | ||
let padding = " ".repeat( | ||
// max length of level | ||
7 | ||
// space after level before target | ||
+ 1, | ||
); | ||
let real = visitor | ||
.other_fields | ||
.iter() | ||
.map(|(key, value)| { | ||
format!( | ||
"{} = {value}", | ||
Style::default().dimmed().italic().paint(key) | ||
) | ||
}) | ||
.collect::<Vec<String>>() | ||
.join(&format!("\n{padding}")); | ||
format!("\n{padding}{real}",) | ||
}; | ||
|
||
let target = event | ||
.metadata() | ||
.target() | ||
.split("::") | ||
.map(|module| Color::Cyan.paint(module).to_string()) | ||
.intersperse_with(|| Color::LightRed.paint("::").to_string()) | ||
.collect::<String>(); | ||
|
||
println!("{level}{pad_level} {target}{message} {other_fields}"); | ||
} | ||
} | ||
|
||
struct CraftVisitor { | ||
message: Option<String>, | ||
other_fields: HashMap<String, String>, | ||
} | ||
|
||
impl CraftVisitor { | ||
fn record(&mut self, field: &Field, value: String) { | ||
if field.name() == "message" { | ||
let _ = self.message.insert(value); | ||
} else { | ||
let _ = self.other_fields.insert(field.name().to_owned(), value); | ||
} | ||
} | ||
} | ||
|
||
impl tracing::field::Visit for CraftVisitor { | ||
fn record_f64(&mut self, field: &tracing::field::Field, value: f64) { | ||
self.record(field, value.to_string()) | ||
} | ||
|
||
fn record_i64(&mut self, field: &tracing::field::Field, value: i64) { | ||
self.record(field, value.to_string()) | ||
} | ||
|
||
fn record_u64(&mut self, field: &tracing::field::Field, value: u64) { | ||
self.record(field, value.to_string()) | ||
} | ||
|
||
fn record_bool(&mut self, field: &tracing::field::Field, value: bool) { | ||
self.record(field, value.to_string()) | ||
} | ||
|
||
fn record_str(&mut self, field: &tracing::field::Field, value: &str) { | ||
self.record(field, value.to_string()) | ||
} | ||
|
||
fn record_error( | ||
&mut self, | ||
field: &tracing::field::Field, | ||
value: &(dyn std::error::Error + 'static), | ||
) { | ||
self.record(field, value.to_string()) | ||
} | ||
|
||
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) { | ||
self.record(field, format!("{value:?}")) | ||
} | ||
} |
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