Skip to main content

mimick/
logging.rs

1use flexi_logger::{DeferredNow, style};
2use log::Record;
3use std::io::Write;
4
5/// Helper to extract formatted filename and line number from logs.
6fn format_log_location(record: &Record) -> String {
7    match (record.file(), record.line()) {
8        (Some(file), Some(line)) => format!(" {}:{}", file, line),
9        _ => String::new(),
10    }
11}
12
13/// Logger formatter that produces plain text for files.
14pub fn detailed_plain_format(
15    w: &mut dyn Write,
16    now: &mut DeferredNow,
17    record: &Record,
18) -> Result<(), std::io::Error> {
19    write!(
20        w,
21        "[{}] {:<5} [{}] {}{}",
22        now.format("%Y-%m-%d %H:%M:%S%.6f %:z"),
23        record.level(),
24        record.target(),
25        record.args(),
26        format_log_location(record)
27    )
28}
29
30/// Logger formatter that produces ANSI color output for terminal displays.
31pub fn detailed_colored_format(
32    w: &mut dyn Write,
33    now: &mut DeferredNow,
34    record: &Record,
35) -> Result<(), std::io::Error> {
36    write!(
37        w,
38        "[{}] {} [{}] {}{}",
39        now.format("%Y-%m-%d %H:%M:%S%.6f %:z"),
40        style(record.level()).paint(format!("{:<5}", record.level())),
41        record.target(),
42        record.args(),
43        format_log_location(record)
44    )
45}