Beispiel #1
0
def test_copy(capsys):
    """We expect copies to initially inherit the log level of their parents."""

    logger1 = Loggable("Parent")
    logger1.set_level("INFO")
    logger2 = logger1.copy("newname")
    assert logger2.name == "newname"
    # logger 1 output
    logger1.info("msg")
    log, _ = capsys.readouterr()
    assert "msg" in log

    # logger 2 output
    logger2.info("msg")
    log, _ = capsys.readouterr()
    assert "msg" in log

    logger1.set_level("ERROR")

    # logger 1 output
    logger1.info("msg")
    log, _ = capsys.readouterr()
    assert not log

    # logger 2 output
    logger2.info("msg")
    log, _ = capsys.readouterr()
    assert "msg" in log
Beispiel #2
0
def test_spawn(capsys):
    """We expect spawned copies to inherit the log level of their parents."""

    logger1 = Loggable("Parent")
    logger1.set_level("INFO")
    logger2 = logger1.spawn("new name")

    # logger 1 output
    logger1.info("msg")
    log, _ = capsys.readouterr()
    assert "msg" in log

    # logger 2 output
    logger2.info("msg")
    log, _ = capsys.readouterr()
    assert "msg" in log

    logger1.set_level("ERROR")

    # logger 1 no output
    logger1.info("msg")
    log, _ = capsys.readouterr()
    assert not log

    # logger 2 no output
    logger2.info("msg")
    log, _ = capsys.readouterr()
    assert not log
Beispiel #3
0
    def test_basic_log(self, capsys):
        logger = Loggable("loggable_test")
        logger.set_level("INFO")

        logger.track("INFO").info("log2")
        log, _ = capsys.readouterr()
        assert "INFO" in log
Beispiel #4
0
 def test_log(self, level, capsys):
     logger = Loggable("test")
     msg = str(uuid4())
     logger.set_level(level)
     logger.log(msg, level)
     log, _ = capsys.readouterr()
     print(log)
     assert msg in log
Beispiel #5
0
    def test_update(self, capsys):
        logger = Loggable("loggable_test")
        logger.set_level("INFO")

        track = logger.track("INFO", total=100).enter()
        track.update(10, "10% there!")
        track.update(20, "20% there!")
        track.exit()
Beispiel #6
0
    def test_no_update(self, capsys):
        logger = Loggable("loggable_test")
        logger.set_level("CRITICAL")
        assert not logger.is_enabled("INFO")

        track = logger.track("INFO", total=100).enter()
        track.update(10, "10% there!")
        track.update(20, "20% there!")
        track.exit()
Beispiel #7
0
    def test_not_enabled(self):
        logger = Loggable("loggable_test")
        logger.set_level("CRITICAL")

        track = logger.track("INFO")
        assert not track.is_enabled()

        track = logger.track("CRITICAL")
        assert track.is_enabled()
Beispiel #8
0
    def test_prefix(self, capsys):
        log = Loggable("loggable_test")
        log.set_level("INFO")

        timeit = log.timeit(logging.INFO, "prefix")
        timeit.enter()
        timeit.info("Some information")
        timeit.exit()
        log, _ = capsys.readouterr()
        print(log)
        assert "prefix" in log
Beispiel #9
0
 def test_spawn(self, capsys):
     log = Loggable("loggable_test")
     log.set_level("INFO")
     log2 = log.timeit(logging.INFO)
     log2.enter()
     log2.info("log2")
     log2.exit()
     msg, _ = capsys.readouterr()
     assert "started" in msg.lower()
     assert "log2" in msg
     assert "finished" in msg.lower()
Beispiel #10
0
    def test_does_not_log(self, capsys):
        log = Loggable("loggable_test")
        log.set_level("ERROR")

        timeit = log.timeit(logging.INFO)
        timeit.enter()
        timeit.info("ok")
        time.sleep(0.1)
        timeit.exit()
        log, _ = capsys.readouterr()
        print(log)
        assert not log
Beispiel #11
0
    def test_timeit(self, capsys):
        log = Loggable("loggable_test")
        log.set_level("INFO")

        timeit = log.timeit(logging.INFO)
        timeit.enter()
        timeit.info("ok")
        time.sleep(0.1)
        timeit.exit()
        log, _ = capsys.readouterr()
        print(log)
        assert "started" in log.lower()
        assert "ok" in log
        assert "finished" in log.lower()
Beispiel #12
0
    def test_does_log(self, level, level_as_str, capsys):
        logger = Loggable("test")

        fxn = getattr(logger, level.lower())

        level_str = level
        if not level_as_str:
            level = getattr(logging, level.upper())
        # check log
        logger.set_level(level)
        msg = str(uuid4())
        fxn(msg)
        log, _ = capsys.readouterr()

        print(log)
        assert msg in log
        assert level_str.upper() in log
Beispiel #13
0
    def test_does_not_log(self, level, capsys):
        logger = Loggable("test")
        fxn = getattr(logger, level.lower())

        # does log
        logger.set_level(level)
        msg = str(uuid4())
        fxn(msg)
        log, _ = capsys.readouterr()
        # print(log)
        assert msg in log
        assert level.upper() in log

        # does not log
        logger.set_level(logging.CRITICAL + 1)
        msg = str(uuid4())
        fxn(msg)
        log, _ = capsys.readouterr()
        assert not log
        assert not _
Beispiel #14
0
def test_pickle_span():

    logger1 = Loggable("Parent")
    logger1.set_level("INFO")
    logger2 = logger1.spawn("new name")

    assert logger1._children
    assert logger2._children == {}

    logger3 = pickle.loads(pickle.dumps(logger1))
    logger4 = pickle.loads(pickle.dumps(logger2))

    assert dict(logger3._children)
    assert logger3._children[logger2._id] is logger2
    assert logger4._children == {}

    for lvl in ["ERROR", "DEBUG", "INFO"]:
        logger1.set_level(lvl)
        assert logger1.level_name() == lvl
        assert logger2.level_name() == lvl
        assert logger3.level_name() == lvl
        assert logger4.level_name() == lvl
Beispiel #15
0
 def test_track_iterable(self):
     logger = Loggable("loggable_test")
     logger.set_level("INFO")
     track = logger.track("INFO", desc="This is a progress bar")
     for x in track(range(10)):
         track.info(x)
Beispiel #16
0
    def test_timeit(self):
        log = Loggable("loggable_test")
        log.set_level("INFO")

        with log.timeit(logging.INFO, "TimeItTest"):
            log.info("ok")