From df171e58091a5ba504bd3269bb217727df5b7d5b Mon Sep 17 00:00:00 2001 From: acid angel from asia Date: Thu, 2 Jan 2025 16:06:47 -0500 Subject: [PATCH] fix double log problem --- conftest.py | 2 +- rose-cli/rose_cli/__main__.py | 2 +- rose-py/rose/__init__.py | 2 +- rose-py/rose/common.py | 28 +++++++++++++++------------- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/conftest.py b/conftest.py index 37662fc..26fc8ab 100644 --- a/conftest.py +++ b/conftest.py @@ -34,7 +34,7 @@ def multiprocessing_set_start_method() -> None: @pytest.fixture(autouse=True) def debug_logging() -> None: - initialize_logging() + initialize_logging(output="stderr") logging.getLogger().setLevel(logging.DEBUG) diff --git a/rose-cli/rose_cli/__main__.py b/rose-cli/rose_cli/__main__.py index 60c3b01..7f47928 100644 --- a/rose-cli/rose_cli/__main__.py +++ b/rose-cli/rose_cli/__main__.py @@ -10,7 +10,7 @@ def main() -> None: multiprocessing.set_start_method("fork") - initialize_logging() + initialize_logging(output="stderr") try: cli() except (RoseExpectedError, CliExpectedError) as e: diff --git a/rose-py/rose/__init__.py b/rose-py/rose/__init__.py index 1931bb2..8e14244 100644 --- a/rose-py/rose/__init__.py +++ b/rose-py/rose/__init__.py @@ -276,4 +276,4 @@ "PlaylistAlreadyExistsError", ] -initialize_logging(__name__) +initialize_logging(__name__, output="file") diff --git a/rose-py/rose/common.py b/rose-py/rose/common.py index 37f110c..f9222e9 100644 --- a/rose-py/rose/common.py +++ b/rose-py/rose/common.py @@ -14,7 +14,7 @@ import unicodedata from collections.abc import Iterator from pathlib import Path -from typing import TYPE_CHECKING, Any, TypeVar +from typing import TYPE_CHECKING, Any, Literal, TypeVar import appdirs @@ -157,7 +157,7 @@ def _rec_sha256_dataclass(hasher: Any, value: Any) -> None: __logging_initialized: set[str | None] = set() -def initialize_logging(logger_name: str | None = None) -> None: +def initialize_logging(logger_name: str | None = None, output: Literal["stderr", "file"] = "stderr") -> None: if logger_name in __logging_initialized: return __logging_initialized.add(logger_name) @@ -188,14 +188,16 @@ def initialize_logging(logger_name: str | None = None) -> None: datefmt="%Y-%m-%d %H:%M:%S", ) - stream_handler = logging.StreamHandler(sys.stderr) - stream_handler.setFormatter(simple_formatter if not log_despite_testing else verbose_formatter) - logger.addHandler(stream_handler) - - file_handler = logging.handlers.RotatingFileHandler( - log_file, - maxBytes=20 * 1024 * 1024, - backupCount=10, - ) - file_handler.setFormatter(verbose_formatter) - logger.addHandler(file_handler) + if output == "stderr": + stream_handler = logging.StreamHandler(sys.stderr) + stream_handler.setFormatter(simple_formatter if not log_despite_testing else verbose_formatter) + logger.addHandler(stream_handler) + + if output == "file": + file_handler = logging.handlers.RotatingFileHandler( + log_file, + maxBytes=20 * 1024 * 1024, + backupCount=10, + ) + file_handler.setFormatter(verbose_formatter) + logger.addHandler(file_handler)