2.1 KiB
Design: Error Location Logging
Date: 2026-03-23 Scope: Add source file and line number to WARN and ERROR log lines
Context
During the first Docker deployment, several runtime errors (non-existent table, VARCHAR constraint violation) produced log lines like ERROR ai_synth_backend::errors: Database error: PgDatabaseError { ... } with no indication of which source file or function triggered the error. Diagnosing required grep-ing the codebase for the SQL string.
Goal
WARN and ERROR log lines include the source file and line number. INFO and DEBUG remain unchanged.
Approach
Create a custom tracing_subscriber event formatter that conditionally appends [file:line] when the event level is WARN or ERROR.
New file: backend/src/logging.rs
~30 lines. Implements FormatEvent trait:
- For WARN/ERROR: appends
[src/file.rs:123]after the target module - For INFO/DEBUG/TRACE: standard format, no file:line
Changes to backend/src/main.rs
Replace:
fmt().with_env_filter(filter).init();
With:
fmt().with_env_filter(filter).event_format(logging::ErrorLocationFormat).init();
Add mod logging; to main.rs (alongside the existing mod cli; — both are binary-only concerns).
Output examples
INFO (unchanged):
2026-03-23T10:00:00Z INFO ai_synth_backend::services::auth: Magic link token created email="test@example.com"
ERROR (with file:line):
2026-03-23T10:00:00Z ERROR ai_synth_backend::errors [src/errors.rs:91]: Database error: PgDatabaseError { ... }
WARN (with file:line):
2026-03-23T10:00:00Z WARN ai_synth_backend::handlers::auth [src/handlers/auth.rs:75]: Rate limit exceeded for email
What does NOT change
- No changes to any
tracing::error!ortracing::warn!call sites - No changes to error types or error handling logic
- No changes to the JSON error response sent to clients
- INFO/DEBUG/TRACE output format unchanged
Testing
cargo test --lib— existing tests pass (formatter is only used in main.rs, not in tests)- Manual: run the app, trigger an error, verify file:line appears in log output