Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(cli): logging (#4)

authored by

Erika and committed by
GitHub
0f8d8d98 cc326cb0

+78 -64
+76 -1
crates/cli/src/logging.rs
··· 1 1 use colored::{ColoredString, Colorize}; 2 - use std::time::{Duration, SystemTimeError}; 2 + use std::{ 3 + fmt, 4 + time::{Duration, SystemTimeError}, 5 + }; 6 + use tracing::{Event, Subscriber}; 7 + use tracing_subscriber::{ 8 + fmt::{format, FmtContext, FormatEvent, FormatFields}, 9 + layer::SubscriberExt, 10 + registry::LookupSpan, 11 + util::SubscriberInitExt, 12 + }; 3 13 4 14 pub struct FormatElapsedTimeOptions<'a> { 5 15 pub(crate) sec_yellow_threshold: u64, ··· 62 72 Ok(result) 63 73 } 64 74 } 75 + 76 + pub struct EventLoggerFormatter; 77 + 78 + impl<S, N> FormatEvent<S, N> for EventLoggerFormatter 79 + where 80 + S: Subscriber + for<'a> LookupSpan<'a>, 81 + N: for<'a> FormatFields<'a> + 'static, 82 + { 83 + fn format_event( 84 + &self, 85 + ctx: &FmtContext<'_, S, N>, 86 + mut writer: format::Writer<'_>, 87 + event: &Event<'_>, 88 + ) -> fmt::Result { 89 + if std::env::args().any(|arg| arg == "--quiet") { 90 + return Ok(()); 91 + } 92 + 93 + if event.metadata().name() == "SKIP_FORMAT" { 94 + ctx.field_format().format_fields(writer.by_ref(), event)?; 95 + return writeln!(writer); 96 + } 97 + 98 + // TODO: Add different formatting for warn, error, etc. 99 + 100 + let timestamp = chrono::Local::now().format("%H:%M:%S").to_string().dimmed(); 101 + let event_name = event.metadata().name(); 102 + 103 + write!( 104 + writer, 105 + "{}{} ", 106 + timestamp, 107 + if event_name.is_empty() { 108 + String::new() 109 + } else { 110 + format!( 111 + " {}", 112 + event_name.to_ascii_lowercase().bold().bright_yellow() 113 + ) 114 + } 115 + )?; 116 + 117 + // Write fields on the event 118 + ctx.field_format().format_fields(writer.by_ref(), event)?; 119 + 120 + if *event.metadata().level() == tracing::Level::ERROR { 121 + // Write the writer to a string so we can colorize it 122 + } 123 + 124 + writeln!(writer) 125 + } 126 + } 127 + 128 + pub fn init_logging() { 129 + let tracing_formatter = tracing_subscriber::fmt::layer().event_format(EventLoggerFormatter); 130 + 131 + tracing_subscriber::registry() 132 + .with( 133 + tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { 134 + format!("{}=info,tower_http=info", env!("CARGO_CRATE_NAME")).into() 135 + }), 136 + ) 137 + .with(tracing_formatter) 138 + .init(); 139 + }
+2 -63
crates/cli/src/main.rs
··· 5 5 use clap::{Parser, Subcommand}; 6 6 use colored::Colorize; 7 7 use dev::coordinate_dev_env; 8 + use logging::init_logging; 8 9 use preview::start_preview_web_server; 9 10 use std::fmt::{self}; 10 11 use std::path::{Path, PathBuf}; ··· 33 34 Preview, 34 35 } 35 36 36 - struct MyFormatter; 37 - 38 - impl<S, N> FormatEvent<S, N> for MyFormatter 39 - where 40 - S: Subscriber + for<'a> LookupSpan<'a>, 41 - N: for<'a> FormatFields<'a> + 'static, 42 - { 43 - fn format_event( 44 - &self, 45 - ctx: &FmtContext<'_, S, N>, 46 - mut writer: format::Writer<'_>, 47 - event: &Event<'_>, 48 - ) -> fmt::Result { 49 - if std::env::args().any(|arg| arg == "--quiet") { 50 - return Ok(()); 51 - } 52 - 53 - if event.metadata().name() == "SKIP_FORMAT" { 54 - ctx.field_format().format_fields(writer.by_ref(), event)?; 55 - return writeln!(writer); 56 - } 57 - 58 - // TODO: Add different formatting for warn, error, etc. 59 - 60 - let timestamp = chrono::Local::now().format("%H:%M:%S").to_string().dimmed(); 61 - let event_name = event.metadata().name(); 62 - 63 - write!( 64 - writer, 65 - "{}{} ", 66 - timestamp, 67 - if event_name.is_empty() { 68 - String::new() 69 - } else { 70 - format!( 71 - " {}", 72 - event_name.to_ascii_lowercase().bold().bright_yellow() 73 - ) 74 - } 75 - )?; 76 - 77 - // Write fields on the event 78 - ctx.field_format().format_fields(writer.by_ref(), event)?; 79 - 80 - if *event.metadata().level() == tracing::Level::ERROR { 81 - // Write the writer to a string so we can colorize it 82 - } 83 - 84 - writeln!(writer) 85 - } 86 - } 87 - 88 37 #[tokio::main] 89 38 async fn main() { 39 + init_logging(); 90 40 let cli = Cli::parse(); 91 - 92 - let tracing_formatter = tracing_subscriber::fmt::layer().event_format(MyFormatter); 93 - 94 - tracing_subscriber::registry() 95 - .with( 96 - tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { 97 - format!("{}=info,tower_http=info", env!("CARGO_CRATE_NAME")).into() 98 - }), 99 - ) 100 - .with(tracing_formatter) 101 - .init(); 102 41 103 42 // You can check for the existence of subcommands, and if found use their 104 43 // matches just as you would the top level cmd