def test_recorder(): th = setup_logging() log = slogging.get_logger() # test info recorder = slogging.LogRecorder() assert len(slogging.log_listeners.listeners) == 1 log.info('a', v=1) assert th.logged r = recorder.pop_records() assert r[0] == dict(event='a', v=1) assert len(slogging.log_listeners.listeners) == 0 # test trace recorder = slogging.LogRecorder() assert len(slogging.log_listeners.listeners) == 1 log.trace('a', v=1) assert not th.logged r = recorder.pop_records() assert r[0] == dict(event='a', v=1) assert len(slogging.log_listeners.listeners) == 0
def test_recorder(caplog): slogging.configure(log_json=True) log = slogging.get_logger() # test info recorder = slogging.LogRecorder() assert len(slogging.log_listeners) == 1 log.info('a', v=1) assert "a" in caplog.text r = recorder.pop_records() assert r[0] == dict(event='a', v=1) assert len(slogging.log_listeners) == 0 # test trace log.setLevel(logging.TRACE) recorder = slogging.LogRecorder() assert len(slogging.log_listeners) == 1 log.trace('a', v=2) assert '"v": 2' in caplog.text r = recorder.pop_records() assert r[0] == dict(event='a', v=2) assert len(slogging.log_listeners) == 0
def test_how_to_use_as_vm_logger(): """ don't log until there was an error """ slogging.configure(':DEBUG,eth.vm:INFO') log = slogging.get_logger('eth.vm') # record all logs def run_vm(raise_error=False): log.trace('op', pc=1) log.trace('op', pc=2) if raise_error: raise Exception recorder = slogging.LogRecorder() try: run_vm(raise_error=True) except BaseException: log = slogging.get_logger('eth.vm') for x in recorder.pop_records(): log.info(x.pop('event'), **x)