Skip to content

Commit

Permalink
log: improve default logging writer
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu committed Jan 16, 2024
1 parent 62f9a30 commit 854c99d
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions crates/log/src/writers/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use serde_json::{json, Value};
use std::{
collections::BTreeMap,
fmt::{self, Debug, Write as _},
process,
process, thread,
};
use tracing::{
field::{Field, Visit},
Expand Down Expand Up @@ -66,19 +66,19 @@ impl<'s> Visit for DefaultVisitor<'s> {
}
}

// time level module (thread name): message
/// Provides a default [`WriteFn`](crate::WriteFn) that is soothing to see in your terminal.
pub fn default(event: &Event, metadata: &Metadata, _spans: Vec<Value>) -> String {
let mut buf = String::new();
let now = Local::now().format("[%B %d, %G - %H:%M:%S %p]");

let now = Local::now().format("%B %d, %G - %H:%M:%S %p");
let (b1, b2) = (
"[".if_supports_color(Stream::Stdout, gray_fg),
"]".if_supports_color(Stream::Stdout, gray_fg),
);

let _ = write!(
buf,
"{} {} {b1}",
"{} {} ",
now.if_supports_color(Stream::Stdout, |x| x.fg_rgb::<134, 134, 134>()),
match *metadata.level() {
Level::TRACE => "TRACE"
Expand Down Expand Up @@ -108,22 +108,15 @@ pub fn default(event: &Event, metadata: &Metadata, _spans: Vec<Value>) -> String
},
);

let target = format!(
"{:>49} {:>25}",
metadata.module_path().unwrap_or("unknown"),
std::thread::current()
.name()
.unwrap_or("main")
.if_supports_color(Stream::Stdout, |x| x.fg_rgb::<244, 181, 213>())
);

let _ = write!(
buf,
"{}",
target.if_supports_color(Stream::Stdout, |x| x.fg_rgb::<120, 231, 255>())
"{} {}{}{}: ",
metadata.module_path().unwrap_or("«unknown»"),
b1,
thread::current().name().unwrap_or("main"),
b2
);

let _ = write!(buf, "{b2} :: ");
let mut visitor = DefaultVisitor {
result: Ok(()),
writer: &mut buf,
Expand Down

0 comments on commit 854c99d

Please sign in to comment.