11Bridge the Python standard-library ``logging`` module into ``oc_diagnostics``
12so that libraries and frameworks that emit log records via ``logging.getLogger``
13are automatically captured and displayed through the unified diagnostics system.
17- Provide ``OcDiagnosticsHandler``, a ``logging.Handler`` subclass that
18 converts each ``LogRecord`` to an ``oc_diagnostics._dbg()`` call.
19- Map Python logging levels to oc_diagnostics diagnostic levels via a
20 configurable ``level_map`` dict.
21- Use a sensible default mapping:
22 DEBUG → L4 (deep tracing)
24 WARNING → L1 (errors only)
30Domain: configurable (default: "GENERAL")
31Levels: determined by level_map at runtime
35- Must not raise; errors are delegated to ``logging.Handler.handleError()``.
36- Requires no optional dependencies (pure Python, always available).
37- The ``domain`` and ``level_map`` are settable at construction time.
46_DEFAULT_LEVEL_MAP: dict[int, int] = {
56 """A ``logging.Handler`` that routes records through ``oc_diagnostics._dbg()``.
61 The oc_diagnostics domain used for all records routed through this
62 handler. Default ``"GENERAL"``.
63 level_map : dict[int, int] | None
64 Mapping from Python ``logging`` level integers to oc_diagnostics
65 diagnostic level integers. When ``None``, the built-in default is
66 used: DEBUG→4, INFO→2, WARNING/ERROR/CRITICAL→1.
73 from oc_diagnostics import OcDiagnosticsHandler
75 handler = OcDiagnosticsHandler(domain="DATA")
76 logging.getLogger("sqlalchemy").addHandler(handler)
81 domain: str =
"GENERAL",
82 level_map: dict[int, int] |
None =
None,
87 level_map
if level_map
is not None else _DEFAULT_LEVEL_MAP
90 def emit(self, record: logging.LogRecord) ->
None:
92 diag_level = self.
_level_map.get(record.levelno, 2)
93 msg = self.format(record)
96 record.funcName
or "",
102 self.handleError(record)
None emit(self, logging.LogRecord record)
None __init__(self, str domain="GENERAL", dict[int, int]|None level_map=None)