feat(cli): always log to a file

This commit is contained in:
Benexl
2025-07-27 11:48:34 +03:00
parent fd74fbe2ef
commit 5ed9700c5c
2 changed files with 32 additions and 13 deletions

View File

@@ -4,7 +4,7 @@ import click
from click.core import ParameterSource
from ..core.config import AppConfig
from ..core.constants import PROJECT_NAME, USER_CONFIG_PATH, __version__
from ..core.constants import PROJECT_NAME, USER_CONFIG, __version__
from .config import ConfigLoader
from .options import options_from_model
from .utils.exception import setup_exceptions_handler
@@ -66,7 +66,7 @@ def cli(ctx: click.Context, **options: "Unpack[Options]"):
"""
The main entry point for the FastAnime CLI.
"""
setup_logging(options["log"], options["log_to_file"])
setup_logging(options["log"])
setup_exceptions_handler(
options["trace"],
options["dev"],
@@ -94,7 +94,7 @@ def cli(ctx: click.Context, **options: "Unpack[Options]"):
cli_overrides[model_name] = {}
cli_overrides[model_name][field_name] = param_value
loader = ConfigLoader(config_path=USER_CONFIG_PATH)
loader = ConfigLoader(config_path=USER_CONFIG)
config = (
AppConfig.model_validate(cli_overrides)
if options["no_config"]

View File

@@ -1,11 +1,16 @@
import logging
from logging.handlers import RotatingFileHandler
from pathlib import Path
from ...core.constants import LOG_FILE_PATH
from ...core.constants import LOG_FILE
logger = logging.getLogger(__name__)
def setup_logging(log: bool | None, log_file: bool | None) -> None:
def setup_logging(log: bool | None) -> None:
"""Configures the application's logging based on CLI flags."""
_setup_default_logger()
if log:
from rich.logging import RichHandler
@@ -16,13 +21,27 @@ def setup_logging(log: bool | None, log_file: bool | None) -> None:
handlers=[RichHandler()],
)
logging.getLogger(__name__).info("Rich logging initialized.")
elif log_file:
logging.basicConfig(
level="DEBUG",
filename=LOG_FILE_PATH,
format="%(asctime)s %(levelname)s: %(message)s",
datefmt="[%d/%m/%Y@%H:%M:%S]",
filemode="w",
)
else:
logging.basicConfig(level="CRITICAL")
def _setup_default_logger(
log_file_path: Path = LOG_FILE,
max_bytes=10 * 1024 * 1024, # 10mb
backup_count=5,
level=logging.DEBUG,
):
logger.setLevel(level)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(process)d - %(thread)d - %(filename)s:%(lineno)d - %(message)s"
)
file_handler = RotatingFileHandler(
log_file_path,
maxBytes=max_bytes,
backupCount=backup_count,
encoding="utf-8",
)
file_handler.setLevel(level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)