2025-01-05 14:41:07 +01:00
|
|
|
"""Settings for logging.
|
|
|
|
|
|
|
|
|
|
Kept in an extra module to avoid cyclic dependencies on package import.
|
|
|
|
|
"""
|
|
|
|
|
|
2025-06-10 22:00:28 +02:00
|
|
|
from pathlib import Path
|
2025-01-05 14:41:07 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
from pydantic import Field, computed_field, field_validator
|
|
|
|
|
|
|
|
|
|
from akkudoktoreos.config.configabc import SettingsBaseModel
|
2025-06-10 22:00:28 +02:00
|
|
|
from akkudoktoreos.core.logabc import LOGGING_LEVELS
|
2025-01-05 14:41:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class LoggingCommonSettings(SettingsBaseModel):
|
2025-01-15 00:54:45 +01:00
|
|
|
"""Logging Configuration."""
|
2025-01-05 14:41:07 +01:00
|
|
|
|
2025-06-10 22:00:28 +02:00
|
|
|
console_level: Optional[str] = Field(
|
|
|
|
|
default=None,
|
|
|
|
|
description="Logging level when logging to console.",
|
|
|
|
|
examples=LOGGING_LEVELS,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
file_level: Optional[str] = Field(
|
|
|
|
|
default=None,
|
|
|
|
|
description="Logging level when logging to file.",
|
|
|
|
|
examples=LOGGING_LEVELS,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@computed_field # type: ignore[prop-decorator]
|
|
|
|
|
@property
|
|
|
|
|
def file_path(self) -> Optional[Path]:
|
|
|
|
|
"""Computed log file path based on data output path."""
|
|
|
|
|
try:
|
|
|
|
|
path = SettingsBaseModel.config.general.data_output_path / "eos.log"
|
|
|
|
|
except:
|
|
|
|
|
# Config may not be fully set up
|
|
|
|
|
path = None
|
|
|
|
|
return path
|
|
|
|
|
|
2025-01-05 14:41:07 +01:00
|
|
|
# Validators
|
2025-06-10 22:00:28 +02:00
|
|
|
@field_validator("console_level", "file_level", mode="after")
|
2025-01-05 14:41:07 +01:00
|
|
|
@classmethod
|
2025-06-10 22:00:28 +02:00
|
|
|
def validate_level(cls, value: Optional[str]) -> Optional[str]:
|
|
|
|
|
"""Validate logging level string."""
|
2025-01-05 14:41:07 +01:00
|
|
|
if value is None:
|
2025-06-10 22:00:28 +02:00
|
|
|
# Nothing to set
|
2025-01-05 14:41:07 +01:00
|
|
|
return None
|
2025-06-10 22:00:28 +02:00
|
|
|
if isinstance(value, str):
|
|
|
|
|
level = value.upper()
|
|
|
|
|
if level == "NONE":
|
|
|
|
|
return None
|
|
|
|
|
if level not in LOGGING_LEVELS:
|
|
|
|
|
raise ValueError(f"Logging level {value} not supported")
|
|
|
|
|
value = level
|
|
|
|
|
else:
|
|
|
|
|
raise TypeError(f"Invalid {type(value)} of logging level {value}")
|
2025-01-05 14:41:07 +01:00
|
|
|
return value
|