Skip to content

Commit 3085b4c

Browse files
committed
Update logger
1 parent bc4d4da commit 3085b4c

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

src/dbx_patch/patches/autoreload_hook_patch.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@
55
and only allows imports from specific paths (like /Workspace). We need to add
66
editable install paths to this allowlist.
77
8-
Additionally, we patch builtins.__import__ itself to ensure editable paths work
9-
even when called through the autoreload hook's original import reference.
8+
Environment Variables:
9+
DBX_PATCH_DEBUG_IMPORTS: Set to '1', 'true', or 'yes' to enable extremely verbose
10+
import tracing by patching builtins.__import__. This logs every single import
11+
that happens in the Python process and should only be used for debugging
12+
import-related issues with editable installs.
13+
14+
Example:
15+
import os
16+
os.environ['DBX_PATCH_DEBUG_IMPORTS'] = '1'
17+
from dbx_patch import patch_dbx
18+
patch_dbx()
1019
"""
1120

1221
import builtins
@@ -103,13 +112,21 @@ def patch(self) -> PatchResult:
103112
"""
104113
logger = self._get_logger()
105114

106-
# Patch builtins.__import__ for debug logging if in debug mode
107-
if logger and logger._logger.isEnabledFor(logging.DEBUG) and not self._import_patch_applied:
108-
logger.info("Debug logging enabled - patching builtins.__import__ for debug logging...")
115+
# Patch builtins.__import__ for debug logging ONLY if explicitly enabled via env var
116+
# This is extremely verbose and should only be used for debugging import issues
117+
import os
118+
119+
if (
120+
os.environ.get("DBX_PATCH_DEBUG_IMPORTS", "").lower() in ("1", "true", "yes")
121+
and not self._import_patch_applied
122+
):
123+
if logger:
124+
logger.info("DBX_PATCH_DEBUG_IMPORTS enabled - patching builtins.__import__ for import tracing...")
109125
self._original_builtins_import = builtins.__import__
110126
builtins.__import__ = self._patched_builtins_import # type: ignore[assignment]
111127
self._import_patch_applied = True
112-
logger.info("builtins.__import__ patched for debug logging")
128+
if logger:
129+
logger.info("builtins.__import__ patched for import tracing (this will be very verbose!)")
113130

114131
if self._is_applied:
115132
if logger:

src/dbx_patch/utils/logger.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class PatchLogger:
1818
Uses composition with logging.Logger and only logs when:
1919
- DBX_PATCH_ENABLED env var is set to true
2020
- DBX_PATCH_LOG_LEVEL env var is set to appropriate level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
21+
22+
Environment Variables:
23+
DBX_PATCH_ENABLED: Set to '1', 'true', or 'yes' to enable logging
24+
DBX_PATCH_LOG_LEVEL: Set to DEBUG, INFO, WARNING, ERROR, or CRITICAL (default: ERROR)
2125
"""
2226

2327
def __init__(self, name: str = "dbx-patch") -> None:
@@ -47,10 +51,15 @@ def __init__(self, name: str = "dbx-patch") -> None:
4751
# Add console handler
4852
handler = logging.StreamHandler(sys.stdout)
4953
handler.setLevel(level)
50-
formatter = logging.Formatter("%(message)s")
51-
handler.setFormatter(formatter)
54+
55+
if self._logger.hasHandlers():
56+
self._logger.handlers.clear()
57+
5258
self._logger.addHandler(handler)
5359

60+
# Prevent propagation to root logger to avoid duplicate messages
61+
self._logger.propagate = False
62+
5463
self._indent_level = 0
5564
self._indent_char = " "
5665

0 commit comments

Comments
 (0)