Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkgs/id/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ fn init_logging(filter: &str, config: &id::structured_logging::LogConfig) {
if config.tz_fell_back {
eprintln!("warning: ID_LOG_TZ value not recognized; log timestamps use UTC");
}
if config.format_fell_back {
eprintln!("warning: ID_LOG_FORMAT value not recognized; logs use text format");
}
let timer = Rfc3339TzTimer { zone: config.zone };

let console_layer: Box<dyn Layer<Registry> + Send + Sync> = match config.format {
Expand Down
38 changes: 37 additions & 1 deletion pkgs/id/src/structured_logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
//! - `ID_LOG_FORMAT` — `text` (default) or `json`. `json` emits one
//! JSON object per line (JSON-lines) including the event's fields and,
//! for events inside a span, the current span's name and fields as
//! trace context.
//! trace context. An unrecognized value falls back to `text` and the
//! caller warns about the fallback (as `ID_LOG_TZ` does).
//! - `ID_LOG_FILE` — path of a second destination. Events are appended
//! to the file in the same format as the console destination, without
//! ANSI escapes.
Expand Down Expand Up @@ -207,6 +208,10 @@ pub struct LogConfig {
pub zone: LogZone,
/// True when `ID_LOG_TZ` was set but unrecognized (zone is UTC).
pub tz_fell_back: bool,
/// True when `ID_LOG_FORMAT` was set to a non-empty value that is
/// neither `text` nor `json` (format is `Text`); the caller warns
/// about the fallback, mirroring `tz_fell_back`.
pub format_fell_back: bool,
}

impl LogConfig {
Expand All @@ -222,6 +227,10 @@ impl LogConfig {
let tz = resolve_zone(tz);
Self {
format: LogFormat::parse(format),
format_fell_back: format.is_some_and(|value| {
let value = value.trim().to_ascii_lowercase();
!value.is_empty() && value != "text" && value != "json"
}),
file: file
.map(str::trim)
.filter(|value| !value.is_empty())
Expand Down Expand Up @@ -345,6 +354,7 @@ mod tests {
assert_eq!(config.file_filter, None);
assert_eq!(config.zone, LogZone::Local);
assert!(!config.tz_fell_back);
assert!(!config.format_fell_back);
}

#[test]
Expand All @@ -356,6 +366,32 @@ mod tests {
assert_eq!(LogFormat::parse(None), LogFormat::Text);
}

#[test]
fn test_structured_logging_unrecognized_format_falls_back_with_flag() {
// An unrecognized ID_LOG_FORMAT still resolves to Text (the
// lenient parse is unchanged), but the config now records the
// fallback so the caller can warn — mirroring tz_fell_back.
for spec in ["josn", "yaml", "jsonl", "structured"] {
let config = LogConfig::resolve(Some(spec), None, None, None);
assert_eq!(config.format, LogFormat::Text, "spec {spec:?}");
assert!(config.format_fell_back, "spec {spec:?}");
}
// Recognized values (any case, padded) and unset/blank values
// never set the flag.
for spec in [
None,
Some(""),
Some(" "),
Some("text"),
Some("Text"),
Some("json"),
Some(" JSON "),
] {
let config = LogConfig::resolve(spec, None, None, None);
assert!(!config.format_fell_back, "spec {spec:?}");
}
}

#[test]
fn structured_logging_utc_timestamp_is_rfc3339_nanos() {
assert_eq!(
Expand Down
Loading