You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2 KiB

use owo_colors::OwoColorize;
use std::fmt;
use tracing::Level;
use tracing_core::{Event, Subscriber};
use tracing_subscriber::fmt::{
format::{self, FormatEvent, FormatFields},
FmtContext,
};
use tracing_subscriber::registry::LookupSpan;
pub struct Formatter;
impl<S, N> FormatEvent<S, N> for Formatter
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: format::Writer<'_>,
event: &Event<'_>,
) -> fmt::Result {
// Format values from the event's's metadata:
let metadata = event.metadata();
write!(
&mut writer,
"{} {}: ",
match *metadata.level() {
Level::ERROR => "ERROR".red().to_string(),
Level::WARN => "WARN".yellow().to_string(),
Level::INFO => "INFO".green().to_string(),
_ => "UNKN".white().to_string(),
},
metadata.target()
)?;
// Write fields on the event
ctx.field_format().format_fields(writer.by_ref(), event)?;
writeln!(writer)
}
}