Skip to content

Commit cb9c5f7

Browse files
committed
refactor: Enhance LoggingSettings initialization to ensure file_path is a Path object and create directories if necessary
1 parent 0c0f11a commit cb9c5f7

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/chromatrace/logging_settings.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class LoggingSettings(BaseModel):
1616
syslog_host: Optional[str] = None
1717
syslog_port: Optional[int] = None
1818

19+
def __init__(self, **data):
20+
super().__init__(**data)
21+
if isinstance(self.file_path, str):
22+
self.file_path = Path(self.file_path)
23+
if isinstance(self.file_path, Path):
24+
self.file_path.mkdir(parents=True, exist_ok=True)
25+
1926

2027
class ColoredFormatter(logging.Formatter):
2128
def __init__(self, fmt=None, datefmt=None, style="%"):
@@ -39,6 +46,7 @@ def format(self, record):
3946
record.name = self._name_color_format.format(record.name)
4047
return super(ColoredFormatter, self).format(record) + self.splitter
4148

49+
4250
class IgnoreNANTraceFormatter(ColoredFormatter):
4351
def format(self, record):
4452
try:
@@ -47,6 +55,7 @@ def format(self, record):
4755
except Exception:
4856
return record.getMessage()
4957

58+
5059
class SysLogFormatter(ColoredFormatter):
5160
def format(self, record):
5261
try:
@@ -64,6 +73,3 @@ def __init__(self, application_level):
6473
def filter(self, record):
6574
record.application_level = self.application_level
6675
return True
67-
68-
69-

0 commit comments

Comments
 (0)