Skip to content

Commit

Permalink
Add a more generic API for notebook logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
ktlim committed Apr 8, 2021
1 parent f78329a commit 26d876a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
26 changes: 25 additions & 1 deletion python/lsst/log/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
# see <https://www.lsstcorp.org/LegalNotices/>.
#

__all__ = ["traceSetAt", "temporaryLogLevel", "LogRedirect"]
__all__ = [
"traceSetAt",
"temporaryLogLevel",
"LogRedirect",
"enable_notebook_logging",
"disable_notebook_logging",
]

from contextlib import contextmanager
import os
Expand Down Expand Up @@ -125,3 +131,21 @@ def finish(self):
os.dup2(self._filehandle, self._fd)
os.close(self._filehandle)
self._thread.join()


_redirect = None


def enable_notebook_logging(dest=sys.stderr):
"""Enable notebook output for log4cxx messages."""
global _redirect
if _redirect is None:
_redirect = LogRedirect(dest=dest)


def disable_notebook_logging():
"""Stop notebook output for log4cxx messages."""
global _redirect
if _redirect is not None:
_redirect.finish()
_redirect = None
29 changes: 16 additions & 13 deletions tests/test_redir.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# LSST Data Management System
# Copyright 2018 LSST Corporation.
#
Expand Down Expand Up @@ -31,11 +30,10 @@
import unittest

import lsst.log as log
import lsst.log.utils as logUtils
import lsst.log.utils as log_utils


class TestRedir(unittest.TestCase):

class StdoutCapture(object):
"""
Context manager to redirect stdout to a file.
Expand Down Expand Up @@ -69,14 +67,14 @@ def tearDown(self):

def check(self, reference):
"""Compare the log file with the provided reference text."""
with open(self.outputFilename, 'r') as f:
with open(self.outputFilename, "r") as f:
# strip everything up to first ] to remove timestamp and thread ID
lines = [l.split(']')[-1].rstrip("\n") for l in f.readlines()]
lines = [ln.split("]")[-1].rstrip("\n") for ln in f.readlines()]
reflines = [rl for rl in reference.split("\n") if rl != ""]
self.maxDiff = None
self.assertListEqual(lines, reflines)

###############################################################################
##########################################################################

def testRedir(self):
"""
Expand All @@ -85,25 +83,30 @@ def testRedir(self):
with TestRedir.StdoutCapture(self.outputFilename):
log.configure()
dest = io.StringIO()
lr = logUtils.LogRedirect(1, dest)
log.log(log.getDefaultLoggerName(), log.INFO, "This is INFO")
log_utils.enable_notebook_logging(dest)
log.getDefaultLogger().log(log.INFO, "This is INFO")
log.info(u"This is unicode INFO")
log.trace("This is TRACE")
log.debug("This is DEBUG")
log.warn("This is WARN")
log.error("This is ERROR")
log.fatal("This is FATAL")
lr.finish()
log_utils.disable_notebook_logging()
log.warn("Format %d %g %s", 3, 2.71828, "foo")
self.assertEqual(dest.getvalue(), """root INFO: This is INFO
self.assertEqual(
dest.getvalue(),
"""root INFO: This is INFO
root INFO: This is unicode INFO
root WARN: This is WARN
root ERROR: This is ERROR
root FATAL: This is FATAL
""")
self.check("""
""",
)
self.check(
"""
root WARN: Format 3 2.71828 foo
""")
"""
)


if __name__ == "__main__":
Expand Down

0 comments on commit 26d876a

Please sign in to comment.