fix: File logging config

This commit is contained in:
Piotr Dec 2025-12-03 23:53:37 +01:00
parent dc1ce2ac46
commit 6efd3457ce
Signed by: stawros
GPG key ID: 74B18A3F0F1E99C0
3 changed files with 6 additions and 3 deletions

View file

@ -1,6 +1,6 @@
logging: logging:
level: "TRACE" level: "TRACE"
path: "logs/karl.log" path: "../../logs/karl.log"
app: app:
host: "127.0.0.1" host: "127.0.0.1"
port: 8081 port: 8081

View file

@ -8,7 +8,7 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
class LoggingConfig(BaseModel): class LoggingConfig(BaseModel):
level: str = "INFO" level: str = "INFO"
path: Path = Path("logs/karl.log") path: Path | None = None
class AppConfig(BaseModel): class AppConfig(BaseModel):

View file

@ -68,7 +68,9 @@ class HandlerFactory:
def file_handler(prefix: str = ''): def file_handler(prefix: str = ''):
if not file_path: if not file_path:
raise ValueError("File path must be set.") import sys
sys.stderr.write("No file path specified, skipping file logging...")
return None
file_path.parent.mkdir(parents=True, exist_ok=True) file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.touch(exist_ok=True) file_path.touch(exist_ok=True)
handler = TimedRotatingFileHandler(file_path, when='midnight', backupCount=30) handler = TimedRotatingFileHandler(file_path, when='midnight', backupCount=30)
@ -87,4 +89,5 @@ class HandlerFactory:
handlers.append(console_handler(handler_prefix)) handlers.append(console_handler(handler_prefix))
case _: case _:
raise ValueError(f"Unknown target: {target}") raise ValueError(f"Unknown target: {target}")
handlers = [h for h in handlers if h is not None]
return handlers return handlers