feat: add source file:line to WARN and ERROR log lines

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
master
oabrivard 3 months ago
parent 004f08f385
commit 6fe75d77e7

@ -0,0 +1,46 @@
//! Custom log formatter that includes source file and line for WARN/ERROR events.
use std::fmt;
use tracing::{Event, Level, Subscriber};
use tracing_subscriber::fmt::format::{self, FormatEvent, FormatFields};
use tracing_subscriber::fmt::FmtContext;
use tracing_subscriber::registry::LookupSpan;
/// Event formatter that appends `[file:line]` to WARN and ERROR log lines.
///
/// INFO, DEBUG, and TRACE events use the default format without source location.
pub struct ErrorLocationFormat;
impl<S, N> FormatEvent<S, N> for ErrorLocationFormat
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 {
let metadata = event.metadata();
let level = metadata.level();
let timestamp = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%S%.6fZ");
// Write: timestamp + level + target
write!(writer, "{} {:>5} {}", timestamp, level, metadata.target())?;
// Append [file:line] for WARN and ERROR only
if *level <= Level::WARN {
if let (Some(file), Some(line)) = (metadata.file(), metadata.line()) {
write!(writer, " [{}:{}]", file, line)?;
}
}
write!(writer, ": ")?;
// Write the event fields (message, structured fields)
ctx.format_fields(writer.by_ref(), event)?;
writeln!(writer)
}
}

@ -4,6 +4,7 @@
//! and starts the HTTP server. Also supports the `create-admin` CLI subcommand.
mod cli;
mod logging;
use anyhow::Context;
use clap::Parser;
@ -27,7 +28,10 @@ async fn main() -> anyhow::Result<()> {
// Initialize tracing
let filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info,ai_synth_backend=debug"));
fmt().with_env_filter(filter).init();
fmt()
.with_env_filter(filter)
.event_format(logging::ErrorLocationFormat)
.init();
let cli = Cli::parse();

Loading…
Cancel
Save