From bb90b2688344d028fb6d10323e362946be53612e Mon Sep 17 00:00:00 2001 From: zhenyus Date: Mon, 21 Apr 2025 13:25:05 +0800 Subject: [PATCH] feat(log): ensure parent directory exists before opening log file Signed-off-by: zhenyus --- apps/authentication/common/log/json_sink.py | 4 ++++ apps/central_storage/common/log/json_sink.py | 4 ++++ apps/content/common/log/json_sink.py | 4 ++++ apps/notification/common/log/json_sink.py | 4 ++++ apps/payment/common/log/json_sink.py | 4 ++++ 5 files changed, 20 insertions(+) diff --git a/apps/authentication/common/log/json_sink.py b/apps/authentication/common/log/json_sink.py index 5c89b31..a798156 100644 --- a/apps/authentication/common/log/json_sink.py +++ b/apps/authentication/common/log/json_sink.py @@ -17,6 +17,10 @@ class JsonSink: self._open_log_file() def _open_log_file(self): + # ensure the parent directory exists + parent_dir = self.log_file_path.parent + if not parent_dir.exists(): + parent_dir.mkdir(parents=True, exist_ok=True) self.log_file = self.log_file_path.open("a", encoding="utf-8") def _should_rotate(self) -> bool: diff --git a/apps/central_storage/common/log/json_sink.py b/apps/central_storage/common/log/json_sink.py index 5c89b31..a798156 100644 --- a/apps/central_storage/common/log/json_sink.py +++ b/apps/central_storage/common/log/json_sink.py @@ -17,6 +17,10 @@ class JsonSink: self._open_log_file() def _open_log_file(self): + # ensure the parent directory exists + parent_dir = self.log_file_path.parent + if not parent_dir.exists(): + parent_dir.mkdir(parents=True, exist_ok=True) self.log_file = self.log_file_path.open("a", encoding="utf-8") def _should_rotate(self) -> bool: diff --git a/apps/content/common/log/json_sink.py b/apps/content/common/log/json_sink.py index 5c89b31..a798156 100644 --- a/apps/content/common/log/json_sink.py +++ b/apps/content/common/log/json_sink.py @@ -17,6 +17,10 @@ class JsonSink: self._open_log_file() def _open_log_file(self): + # ensure the parent directory exists + parent_dir = self.log_file_path.parent + if not parent_dir.exists(): + parent_dir.mkdir(parents=True, exist_ok=True) self.log_file = self.log_file_path.open("a", encoding="utf-8") def _should_rotate(self) -> bool: diff --git a/apps/notification/common/log/json_sink.py b/apps/notification/common/log/json_sink.py index 5c89b31..a798156 100644 --- a/apps/notification/common/log/json_sink.py +++ b/apps/notification/common/log/json_sink.py @@ -17,6 +17,10 @@ class JsonSink: self._open_log_file() def _open_log_file(self): + # ensure the parent directory exists + parent_dir = self.log_file_path.parent + if not parent_dir.exists(): + parent_dir.mkdir(parents=True, exist_ok=True) self.log_file = self.log_file_path.open("a", encoding="utf-8") def _should_rotate(self) -> bool: diff --git a/apps/payment/common/log/json_sink.py b/apps/payment/common/log/json_sink.py index 5c89b31..a798156 100644 --- a/apps/payment/common/log/json_sink.py +++ b/apps/payment/common/log/json_sink.py @@ -17,6 +17,10 @@ class JsonSink: self._open_log_file() def _open_log_file(self): + # ensure the parent directory exists + parent_dir = self.log_file_path.parent + if not parent_dir.exists(): + parent_dir.mkdir(parents=True, exist_ok=True) self.log_file = self.log_file_path.open("a", encoding="utf-8") def _should_rotate(self) -> bool: