From 499d5622d6faae75fbde8509a5d362cd199c975f Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Fri, 30 Dec 2022 14:17:23 -0500 Subject: [PATCH] Simplify logging (#209) --- ffmpeg_normalize/__main__.py | 2 +- ffmpeg_normalize/_cmd_utils.py | 2 +- ffmpeg_normalize/_errors.py | 2 +- ffmpeg_normalize/_ffmpeg_normalize.py | 2 +- ffmpeg_normalize/_logger.py | 55 +++++++++------------------ ffmpeg_normalize/_media_file.py | 2 +- ffmpeg_normalize/_streams.py | 2 +- 7 files changed, 25 insertions(+), 42 deletions(-) diff --git a/ffmpeg_normalize/__main__.py b/ffmpeg_normalize/__main__.py index b0232a1..449b183 100644 --- a/ffmpeg_normalize/__main__.py +++ b/ffmpeg_normalize/__main__.py @@ -13,7 +13,7 @@ from ._logger import setup_custom_logger from ._version import __version__ -logger = setup_custom_logger("ffmpeg_normalize") +logger = setup_custom_logger() def create_parser() -> argparse.ArgumentParser: diff --git a/ffmpeg_normalize/_cmd_utils.py b/ffmpeg_normalize/_cmd_utils.py index 5a9049a..8b3cfe5 100644 --- a/ffmpeg_normalize/_cmd_utils.py +++ b/ffmpeg_normalize/_cmd_utils.py @@ -13,7 +13,7 @@ from ._errors import FFmpegNormalizeError from ._logger import setup_custom_logger -logger = setup_custom_logger("ffmpeg_normalize") +logger = setup_custom_logger() NUL = "NUL" if system() in ("Windows", "cli") else "/dev/null" DUR_REGEX = re.compile( diff --git a/ffmpeg_normalize/_errors.py b/ffmpeg_normalize/_errors.py index 06b9b37..258fd7e 100644 --- a/ffmpeg_normalize/_errors.py +++ b/ffmpeg_normalize/_errors.py @@ -3,7 +3,7 @@ from ._logger import setup_custom_logger -logger = setup_custom_logger("ffmpeg_normalize") +logger = setup_custom_logger() class FFmpegNormalizeError(Exception): diff --git a/ffmpeg_normalize/_ffmpeg_normalize.py b/ffmpeg_normalize/_ffmpeg_normalize.py index 43d014b..85c51ac 100644 --- a/ffmpeg_normalize/_ffmpeg_normalize.py +++ b/ffmpeg_normalize/_ffmpeg_normalize.py @@ -14,7 +14,7 @@ if TYPE_CHECKING: from ._streams import LoudnessStatisticsWithMetadata -logger = setup_custom_logger("ffmpeg_normalize") +logger = setup_custom_logger() NORMALIZATION_TYPES = ("ebu", "rms", "peak") PCM_INCOMPATIBLE_FORMATS = {"flac", "mp3", "mp4", "ogg", "oga", "opus", "webm"} diff --git a/ffmpeg_normalize/_logger.py b/ffmpeg_normalize/_logger.py index 885bb3d..decc04d 100644 --- a/ffmpeg_normalize/_logger.py +++ b/ffmpeg_normalize/_logger.py @@ -6,7 +6,7 @@ from tqdm import tqdm -loggers: dict[str, logging.Logger] = {} +_global_log: logging.Logger | None = None # https://stackoverflow.com/questions/38543506/ @@ -14,7 +14,7 @@ class TqdmLoggingHandler(logging.StreamHandler): def __init__(self) -> None: super().__init__(sys.stderr) - def emit(self, record) -> None: + def emit(self, record: logging.LogRecord) -> None: try: msg = self.format(record) set_mp_lock() @@ -37,21 +37,10 @@ def set_mp_lock() -> None: pass -def setup_custom_logger(name: str) -> logging.Logger: +def setup_custom_logger() -> logging.Logger: """ - Create a logger with a certain name and level + Grab or create the global logger """ - global loggers - - existing_logger = loggers.get(name) - if existing_logger is not None: - return existing_logger - - formatter = logging.Formatter(fmt="%(levelname)s: %(message)s") - - # handler = logging.StreamHandler() - handler = TqdmLoggingHandler() - handler.setFormatter(formatter) # \033[1;30m - black # \033[1;31m - red @@ -62,29 +51,23 @@ def setup_custom_logger(name: str) -> logging.Logger: # \033[1;36m - cyan # \033[1;37m - white - if system() not in ["Windows", "cli"]: - logging.addLevelName( - logging.ERROR, f"{logging.getLevelName(logging.ERROR)}" - ) - logging.addLevelName( - logging.WARNING, - f"{logging.getLevelName(logging.WARNING)}", - ) - logging.addLevelName( - logging.INFO, f"{logging.getLevelName(logging.INFO)}" - ) - logging.addLevelName( - logging.DEBUG, f"{logging.getLevelName(logging.DEBUG)}" - ) - - logger = logging.getLogger(name) + global _global_log + if _global_log is not None: + return _global_log + + if system() not in ("Windows", "cli"): + logging.addLevelName(logging.ERROR, "ERROR") + logging.addLevelName(logging.WARNING, "WARNING") + logging.addLevelName(logging.INFO, "INFO") + logging.addLevelName(logging.DEBUG, "DEBUG") + + logger = logging.Logger("ffmpeg_normalize") logger.setLevel(logging.WARNING) - # if (logger.hasHandlers()): - # logger.handlers.clear() - if logger.handlers: - logger.handlers = [] + handler = TqdmLoggingHandler() + handler.setFormatter(logging.Formatter(fmt="%(levelname)s: %(message)s")) logger.addHandler(handler) - loggers.update(dict(name=logger)) + + _global_log = logger return logger diff --git a/ffmpeg_normalize/_media_file.py b/ffmpeg_normalize/_media_file.py index c0d06a1..18cfbd0 100644 --- a/ffmpeg_normalize/_media_file.py +++ b/ffmpeg_normalize/_media_file.py @@ -17,7 +17,7 @@ if TYPE_CHECKING: from ffmpeg_normalize import FFmpegNormalize -logger = setup_custom_logger("ffmpeg_normalize") +logger = setup_custom_logger() AUDIO_ONLY_FORMATS = {"aac", "ast", "flac", "mp3", "mka", "oga", "ogg", "opus", "wav"} ONE_STREAM = {"aac", "ast", "flac", "mp3", "wav"} diff --git a/ffmpeg_normalize/_streams.py b/ffmpeg_normalize/_streams.py index 8e00125..e98d2c8 100644 --- a/ffmpeg_normalize/_streams.py +++ b/ffmpeg_normalize/_streams.py @@ -13,7 +13,7 @@ from ._ffmpeg_normalize import FFmpegNormalize from ._media_file import MediaFile -logger = setup_custom_logger("ffmpeg_normalize") +logger = setup_custom_logger() class EbuLoudnessStatistics(TypedDict): input_i: float