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.
This commit is contained in:
Wez Furlong
2025-02-25 12:23:50 -07:00
parent 2e4dbd8c5d
commit 39f6aede9e
2 changed files with 35 additions and 2 deletions
+33 -2
View File
@@ -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)
+2
View File
@@ -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