Skip to content

Commit

Permalink
Decomposed set_up_rich_handler function
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbraza committed Sep 27, 2024
1 parent fe105b9 commit 01af51b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions paperqa/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@
"LiteLLM": logging.DEBUG, # <-- every single LLM call
}

_PAPERQA_ROOT_LOGGER = logging.getLogger(__name__.split(".", maxsplit=1)[0])


def is_running_under_cli() -> bool:
"""Check if the current Python process comes from the CLI."""
return any(isinstance(h, RichHandler) for h in _PAPERQA_ROOT_LOGGER.handlers)

def configure_cli_logging(verbosity: int = 0) -> None:
"""Suppress loquacious loggers according to verbosity level."""
setup_default_logs()

def set_up_rich_handler() -> RichHandler:
"""Add a RichHandler to the paper-qa "root" logger, and return it."""
rich_handler = RichHandler(
rich_tracebacks=True,
markup=True,
Expand All @@ -66,10 +71,15 @@ def configure_cli_logging(verbosity: int = 0) -> None:
console=Console(force_terminal=True),
)
rich_handler.setFormatter(logging.Formatter("%(message)s", datefmt="[%X]"))
if not is_running_under_cli():
_PAPERQA_ROOT_LOGGER.addHandler(rich_handler)
return rich_handler

module_logger = logging.getLogger(__name__.split(".", maxsplit=1)[0])
if not any(isinstance(h, RichHandler) for h in module_logger.handlers):
module_logger.addHandler(rich_handler)

def configure_cli_logging(verbosity: int = 0) -> None:
"""Suppress loquacious loggers according to verbosity level."""
setup_default_logs()
set_up_rich_handler()

for logger_name, logger_ in logging.Logger.manager.loggerDict.items():
if isinstance(logger_, logging.Logger) and (
Expand Down

0 comments on commit 01af51b

Please sign in to comment.