From 8cc4c96c183791ad94dda283068513cde7850e87 Mon Sep 17 00:00:00 2001 From: blepabyte <255@blepabyte.me> Date: Mon, 7 Oct 2024 10:56:47 +1300 Subject: [PATCH] iter: bind internal context for logging --- maxray/__init__.py | 21 +++------------------ maxray/function_store.py | 2 +- maxray/logging.py | 3 +++ maxray/nodes.py | 3 +-- 4 files changed, 8 insertions(+), 21 deletions(-) create mode 100644 maxray/logging.py diff --git a/maxray/__init__.py b/maxray/__init__.py index fac73de..9ebb1ec 100644 --- a/maxray/__init__.py +++ b/maxray/__init__.py @@ -12,27 +12,12 @@ import os -from loguru import logger +from loguru import logger as _logger +from .logging import logger if not os.environ.get("MAXRAY_LOG_LEVEL"): # Avoid logspam for users of the library - logger.disable("maxray") - - -def _set_logging(enabled: bool): - if enabled: - logger.enable("maxray") - else: - logger.disable("maxray") - - -@contextmanager -def _with_logging(): - try: - _set_logging(True) - yield - finally: - _set_logging(False) + _logger.disable("maxray") def transform(writer): diff --git a/maxray/function_store.py b/maxray/function_store.py index 3b9aeb8..a290091 100644 --- a/maxray/function_store.py +++ b/maxray/function_store.py @@ -13,7 +13,7 @@ from pathlib import Path from typing import Any, Optional -from loguru import logger +from .logging import logger method_wrapper = type(object().__init__) diff --git a/maxray/logging.py b/maxray/logging.py new file mode 100644 index 0000000..137e7ce --- /dev/null +++ b/maxray/logging.py @@ -0,0 +1,3 @@ +from loguru import logger as _logger + +logger = _logger.bind(maxray_internal=True) diff --git a/maxray/nodes.py b/maxray/nodes.py index d7a06fd..fe0a670 100644 --- a/maxray/nodes.py +++ b/maxray/nodes.py @@ -8,7 +8,7 @@ from typing import Any, Callable, Optional -from loguru import logger +from .logging import logger @dataclass @@ -21,7 +21,6 @@ class FnContext: line_offset: int "Line number in `source_file` at which the function definition begins." - call_count: ContextVar[int] compile_id: str def __repr__(self):