def test_logging_default(): """ Test default logging configuration. """ d = logging.getLogger('distributed') assert len(d.handlers) == 1 assert isinstance(d.handlers[0], logging.StreamHandler) # Work around Bokeh messing with the root logger level # https://github.com/bokeh/bokeh/issues/5793 root = logging.getLogger('') old_root_level = root.level root.setLevel('WARN') for handler in d.handlers: handler.setLevel('INFO') try: dfb = logging.getLogger('distributed.foo.bar') f = logging.getLogger('foo') fb = logging.getLogger('foo.bar') with captured_handler(d.handlers[0]) as distributed_log: with captured_logger(root, level=logging.ERROR) as foreign_log: h = logging.StreamHandler(foreign_log) fmt = '[%(levelname)s in %(name)s] - %(message)s' h.setFormatter(logging.Formatter(fmt)) fb.addHandler(h) fb.propagate = False # For debugging dump_logger_list() d.debug("1: debug") d.info("2: info") dfb.info("3: info") fb.info("4: info") fb.error("5: error") f.info("6: info") f.error("7: error") distributed_log = distributed_log.getvalue().splitlines() foreign_log = foreign_log.getvalue().splitlines() # distributed log is configured at INFO level by default assert distributed_log == [ "distributed - INFO - 2: info", "distributed.foo.bar - INFO - 3: info", ] # foreign logs should be unaffected by distributed's logging # configuration. They get the default ERROR level from logging. assert foreign_log == [ "[ERROR in foo.bar] - 5: error", "7: error", ] finally: root.setLevel(old_root_level)
def test_logging(): """ Test default logging configuration. """ d = logging.getLogger('distributed') assert len(d.handlers) == 1 assert isinstance(d.handlers[0], logging.StreamHandler) root = logging.getLogger('') dfb = logging.getLogger('distributed.foo.bar') f = logging.getLogger('foo') fb = logging.getLogger('foo.bar') with captured_handler(d.handlers[0]) as distributed_log: with captured_logger(root) as foreign_log: h = logging.StreamHandler(foreign_log) fmt = '[%(levelname)s in %(name)s] - %(message)s' h.setFormatter(logging.Formatter(fmt)) fb.addHandler(h) fb.propagate = False d.debug("1: debug") d.info("2: info") dfb.info("3: info") fb.info("4: info") fb.error("5: error") f.info("6: info") f.error("7: error") distributed_log = distributed_log.getvalue().splitlines() foreign_log = foreign_log.getvalue().splitlines() # distributed log is configured at INFO level by default assert distributed_log == [ "distributed - INFO - 2: info", "distributed.foo.bar - INFO - 3: info", ] # foreign logs should be unaffected by distributed's logging # configuration. They get the default ERROR level from logging. assert foreign_log == [ "[ERROR in foo.bar] - 5: error", "7: error", ]
def test_logging_default(): """ Test default logging configuration. """ d = logging.getLogger("distributed") assert len(d.handlers) == 1 assert isinstance(d.handlers[0], logging.StreamHandler) # Work around Bokeh messing with the root logger level # https://github.com/bokeh/bokeh/issues/5793 root = logging.getLogger("") old_root_level = root.level root.setLevel("WARN") for handler in d.handlers: handler.setLevel("INFO") try: dfb = logging.getLogger("distributed.foo.bar") f = logging.getLogger("foo") fb = logging.getLogger("foo.bar") with captured_handler(d.handlers[0]) as distributed_log: with captured_logger(root, level=logging.ERROR) as foreign_log: h = logging.StreamHandler(foreign_log) fmt = "[%(levelname)s in %(name)s] - %(message)s" h.setFormatter(logging.Formatter(fmt)) fb.addHandler(h) fb.propagate = False # For debugging dump_logger_list() d.debug("1: debug") d.info("2: info") dfb.info("3: info") fb.info("4: info") fb.error("5: error") f.info("6: info") f.error("7: error") distributed_log = distributed_log.getvalue().splitlines() foreign_log = foreign_log.getvalue().splitlines() # Filter out asctime pattern = re.compile( r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d* - (.*)$") without_timestamps = [] for msg in distributed_log: m = re.match(pattern, msg) if m: without_timestamps.append(m.group(1)) else: raise AssertionError(f"Unknown format encountered {msg}") # distributed log is configured at INFO level by default assert without_timestamps == [ "distributed - INFO - 2: info", "distributed.foo.bar - INFO - 3: info", ] # foreign logs should be unaffected by distributed's logging # configuration. They get the default ERROR level from logging. assert foreign_log == ["[ERROR in foo.bar] - 5: error", "7: error"] finally: root.setLevel(old_root_level)