Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] Set log level depending on in-notebook or not, and use stdout #1150

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions apis/python/src/tiledbsoma/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
# Licensed under the MIT License.

import logging
import sys
from typing import Optional

logger = logging.getLogger("tiledbsoma")


def warning() -> None:
"""Sets ``tiledbsoma.logging`` to a WARNING level.
Expand Down Expand Up @@ -48,7 +47,7 @@ def _set_level(level: int) -> None:
# ``tiledbsoma.logging.info()`` then ``tiledbsoma.logging.debug()``, etc., then log messages will appear
# twice.
if not logger.hasHandlers():
logger.addHandler(logging.StreamHandler())
logger.addHandler(logging.StreamHandler(stream=sys.stdout))


def log_io(info_message: Optional[str], debug_message: str) -> None:
Expand All @@ -66,3 +65,29 @@ def log_io(info_message: Optional[str], debug_message: str) -> None:
elif logger.level <= logging.DEBUG:
if debug_message is not None:
logger.debug(debug_message)


def _in_notebook() -> bool:
try:
from IPython import get_ipython

if "IPKernelApp" not in get_ipython().config: # pragma: no cover
return False
except ImportError:
return False
except AttributeError:
return False
return True


logger = logging.getLogger("tiledbsoma")
# For interactive notebook use it's _crucial_ that data ingests (which can take several minutes)
# must make a progress indicator to show something is happening -- by default and with zero user
# intervention.
#
# Additional note: without this first _set_level, default logging will go to stderr. We want it to
# stdout.
if _in_notebook():
_set_level(logging.INFO)
else:
_set_level(logging.WARN)