Skip to content

Commit

Permalink
Enable logging in jupyter and non-jupyter contexts.
Browse files Browse the repository at this point in the history
- In jupyter, log to std out with no format string (just like print)
- In non-jupyter, do the same. But need to override Prefect's logging
  handler (which writes to stderr stream) to see the output in the
  worker stdout.
- Accept the fact that only my logger output (from core pacakge) will
  not be visible in the prefect UI. For that need to use print
  statements or prefect logger in flow/task code.
  • Loading branch information
domoench committed Jul 19, 2024
1 parent 021a170 commit 9acf8f6
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 976 deletions.
42 changes: 24 additions & 18 deletions core/logging.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
from prefect import get_run_logger
from prefect.exceptions import MissingContextError
import logging
import sys
from IPython import get_ipython


class Logger:
"""Rudimentary logger that logs to prefect if in a prefect context,
and to print statements if not (e.g. in a notebook)"""
def __init__(self):
try:
self.logger = get_run_logger()
except MissingContextError:
print('Not logging in Prefect context')
self.logger = None
def in_jupyter():
try:
if 'IPKernelApp' in get_ipython().config:
return True
except AttributeError:
pass
return False

def info(self, s):
if self.logger:
self.logger.info(s)
else:
print(s)

# Singleton logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# Singleton logger instance
logger = Logger()
# Configure handlers
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
stream_handler.setFormatter(formatter)
if not in_jupyter():
# In the prefect context they add their own handler to stderr.
# Remove that so I can see log lines from my core module
for handler in logger.handlers:
logger.removeHandler(handler)
logger.addHandler(stream_handler)


def get_logger():
Expand Down
Loading

0 comments on commit 9acf8f6

Please sign in to comment.