Skip to content

Commit

Permalink
properly initialize the loggers with package name
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed May 6, 2024
1 parent 6a648a6 commit f09045e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
63 changes: 3 additions & 60 deletions rose/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import logging
import logging.handlers
import os
import sys
from pathlib import Path

import appdirs

from rose.audiotags import (
SUPPORTED_AUDIO_EXTENSIONS,
AudioTags,
Expand Down Expand Up @@ -57,6 +49,7 @@
VERSION,
RoseError,
RoseExpectedError,
initialize_logging,
sanitize_dirname,
sanitize_filename,
)
Expand Down Expand Up @@ -177,57 +170,7 @@
"toggle_release_new",
"update_cache",
"update_cache_for_releases",
"initialize_logging",
]

__logging_initialized = False


def initialize_logging() -> None:
global __logging_initialized
if __logging_initialized:
return
__logging_initialized = True

logger = logging.getLogger()
logger.setLevel(logging.INFO)

# appdirs by default has Unix log to $XDG_CACHE_HOME, but I'd rather write logs to $XDG_STATE_HOME.
log_home = Path(appdirs.user_state_dir("rose"))
if appdirs.system == "darwin":
log_home = Path(appdirs.user_log_dir("rose"))

log_home.mkdir(parents=True, exist_ok=True)
log_file = log_home / "rose.log"

# Useful for debugging problems with the virtual FS, since pytest doesn't capture that debug logging
# output.
log_despite_testing = os.environ.get("LOG_TEST", False)

# Add a logging handler for stdout unless we are testing. Pytest
# captures logging output on its own, so by default, we do not attach our own.
if "pytest" not in sys.modules or log_despite_testing: # pragma: no cover
simple_formatter = logging.Formatter(
"[%(asctime)s] %(levelname)s: %(message)s",
datefmt="%H:%M:%S",
)
verbose_formatter = logging.Formatter(
"[ts=%(asctime)s.%(msecs)03d] [pid=%(process)d] [src=%(name)s:%(lineno)s] %(levelname)s: %(message)s",
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)


initialize_logging()
initialize_logging(__name__)
57 changes: 57 additions & 0 deletions rose/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@

import dataclasses
import hashlib
import logging
import logging.handlers
import os
import os.path
import re
import sys
from collections.abc import Iterator
from pathlib import Path
from typing import TYPE_CHECKING, Any, TypeVar

import appdirs

if TYPE_CHECKING:
from rose.config import Config

Expand Down Expand Up @@ -135,3 +141,54 @@ def _rec_sha256_dataclass(hasher: Any, value: Any) -> None:
_rec_sha256_dataclass(hasher, v)
else:
hasher.update(str(value).encode())


__logging_initialized = False


def initialize_logging(logger_name: str) -> None:
global __logging_initialized
if __logging_initialized:
return
__logging_initialized = True

logger = logging.getLogger(logger_name)
logger.setLevel(logging.INFO)

# appdirs by default has Unix log to $XDG_CACHE_HOME, but I'd rather write logs to $XDG_STATE_HOME.
log_home = Path(appdirs.user_state_dir("rose"))
if appdirs.system == "darwin":
log_home = Path(appdirs.user_log_dir("rose"))

log_home.mkdir(parents=True, exist_ok=True)
log_file = log_home / "rose.log"

# Useful for debugging problems with the virtual FS, since pytest doesn't capture that debug logging
# output.
log_despite_testing = os.environ.get("LOG_TEST", False)

# Add a logging handler for stdout unless we are testing. Pytest
# captures logging output on its own, so by default, we do not attach our own.
if "pytest" not in sys.modules or log_despite_testing: # pragma: no cover
simple_formatter = logging.Formatter(
"[%(asctime)s] %(levelname)s: %(message)s",
datefmt="%H:%M:%S",
)
verbose_formatter = logging.Formatter(
"[ts=%(asctime)s.%(msecs)03d] [pid=%(process)d] [src=%(name)s:%(lineno)s] %(levelname)s: %(message)s",
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)
2 changes: 1 addition & 1 deletion rose_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from rose import initialize_logging

initialize_logging()
initialize_logging(__name__)
2 changes: 1 addition & 1 deletion rose_vfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"unmount_virtualfs",
]

initialize_logging()
initialize_logging(__name__)

0 comments on commit f09045e

Please sign in to comment.