From 39f6aede9ee2a425cff3d6a17623d23e42b2a5ac Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 25 Feb 2025 12:23:50 -0700 Subject: [PATCH] logging: create sub-directory structure if needed previously, we assumed that only the parent log_dir might be the thing that would need to be created on demand. If you configure per-record log files with subdirectories in them, and the indicated directory didn't already exist, then we'd error out. This commit will catch the error in that case and try to fill out the directory structure on demand, which makes things a bit more convenient. --- crates/kumod/src/logging/files.rs | 35 +++++++++++++++++++++++++++++-- docs/changelog/main.md | 2 ++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/crates/kumod/src/logging/files.rs b/crates/kumod/src/logging/files.rs index 7873c858..a1a94677 100644 --- a/crates/kumod/src/logging/files.rs +++ b/crates/kumod/src/logging/files.rs @@ -276,12 +276,43 @@ impl LogThreadState { } let name = file_key.log_dir.join(base_name); + // They might be trying to use multiple directories below + // the configured log_dir, so adjust our idea of its parent + // dir + let log_dir = name + .parent() + .expect("log_dir.join ensures we always have a parent"); - let f = std::fs::OpenOptions::new() + let f = match std::fs::OpenOptions::new() .append(true) .create(true) .open(&name) - .with_context(|| format!("open log file {name:?}"))?; + { + Ok(f) => f, + Err(err) => { + if err.kind() == std::io::ErrorKind::NotFound { + match std::fs::create_dir_all(&log_dir) { + Ok(_) => { + // Try opening it again now + std::fs::OpenOptions::new() + .append(true) + .create(true) + .open(&name) + .with_context(|| format!("open log file {name:?}"))? + } + Err(dir_err) => { + anyhow::bail!( + "open log file {name:?}: failed: {err:#?}. \ + Additionally, attempting to create dir {} failed: {dir_err:#?}", + log_dir.display() + ); + } + } + } else { + anyhow::bail!("open log file {name:?}: failed: {err:#?}"); + } + } + }; let mut file = OpenedFile { file: Encoder::new(f, self.params.compression_level) diff --git a/docs/changelog/main.md b/docs/changelog/main.md index f5e319e9..5a9396f0 100644 --- a/docs/changelog/main.md +++ b/docs/changelog/main.md @@ -38,6 +38,8 @@ allows setting the underlying [back_pressure](../reference/kumo/configure_log_hook.md) for the TSA log hooks. +* local logger will now create sub-directories if they do not already exist + below the configured log directory. ## Fixes