-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path_logging.py
More file actions
39 lines (28 loc) · 1.23 KB
/
_logging.py
File metadata and controls
39 lines (28 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import inspect
import logging
import pooch
from loguru import logger
class _InterceptHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
# Get corresponding Loguru level if it exists.
level: str | int
try:
level = logger.level(record.levelname).name
except ValueError: # pragma: no cover
level = record.levelno
# Find caller from where originated the logged message.
frame, depth = inspect.currentframe(), 0
while frame and (depth == 0 or frame.f_code.co_filename == logging.__file__):
frame = frame.f_back
depth += 1
logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())
def capture_logging() -> None:
"""
Capture logging from the standard library and redirect it to Loguru
Note that this replaces the root logger, so any other handlers attached to it will be removed.
"""
# Pooch adds a handler to its own logger which circumvents the REF logger
pooch.get_logger().handlers.clear()
pooch.get_logger().addHandler(_InterceptHandler())
logging.basicConfig(handlers=[_InterceptHandler()], level=0, force=True)
__all__ = ["capture_logging", "logger"]