Skip to content

Commit

Permalink
fix double log problem
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Jan 2, 2025
1 parent 54b2345 commit df171e5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion rose-cli/rose_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion rose-py/rose/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,4 @@
"PlaylistAlreadyExistsError",
]

initialize_logging(__name__)
initialize_logging(__name__, output="file")
28 changes: 15 additions & 13 deletions rose-py/rose/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit df171e5

Please sign in to comment.