Skip to content

Commit 0d681d5

Browse files
committed
Show intake-esgf log messages
1 parent 468ec61 commit 0d681d5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

scripts/fetch_test_data.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import inspect
2+
import logging
13
import pathlib
24
from pathlib import Path
35

@@ -423,6 +425,32 @@ def process_sample_data_request(
423425
]
424426

425427

428+
# Copied from https://github.com/Delgan/loguru#entirely-compatible-with-standard-logging
429+
class InterceptHandler(logging.Handler):
430+
"""Intercepts standard logging messages and redirects them to Loguru."""
431+
432+
def emit(self, record: logging.LogRecord) -> None:
433+
"""Emit a record."""
434+
# Get corresponding Loguru level if it exists.
435+
try:
436+
level: str | int = logger.level(record.levelname).name
437+
except ValueError:
438+
level = record.levelno
439+
440+
# Find caller from where originated the logged message.
441+
frame, depth = inspect.currentframe(), 0
442+
while frame:
443+
filename = frame.f_code.co_filename
444+
is_logging = filename == logging.__file__
445+
is_frozen = "importlib" in filename and "_bootstrap" in filename
446+
if depth > 0 and not (is_logging or is_frozen):
447+
break
448+
frame = frame.f_back
449+
depth += 1
450+
451+
logger.opt(depth=depth + 2, exception=record.exc_info).log(level, record.getMessage())
452+
453+
426454
@app.command()
427455
def create_sample_data(
428456
decimate: bool = True,
@@ -432,6 +460,7 @@ def create_sample_data(
432460
run_sequentially: bool = False,
433461
) -> None:
434462
"""Fetch and create sample datasets"""
463+
logging.basicConfig(handlers=[InterceptHandler()], force=True)
435464
processed_datasets = pd.DataFrame(columns=["source_type", "key", "files", "time_start", "time_end"])
436465

437466
if run_sequentially:

0 commit comments

Comments
 (0)