def test_add_handler_bad_level(self): w = Wryte(name=str(uuid.uuid4()), bare=True) w.add_handler(handler=logging.StreamHandler(sys.stdout), name='_json', formatter='json', level='BOOBOO') assert len(w.list_handlers()) == 0
def test_add_handler_bad_level(self): w = Wryte(name=str(uuid.uuid4()), bare=True) with pytest.raises(ValueError): w.add_handler(handler=logging.StreamHandler(sys.stdout), name='_json', formatter='json', level='BOOBOO')
def test_event(self): w = Wryte(name=str(uuid.uuid4())) cid = w.event('Event', cid='user_id') assert cid == 'user_id' cid = w.event('Event') assert len(cid) == len(str(uuid.uuid4()))
def test_simple_context(self): w = Wryte(color=False) timing = [] for _ in range(5): now = datetime.now() for _ in range(10): w.info('My Message', {'key': 'value'}) timing.append((datetime.now() - now).total_seconds() * 1000.0) # This is just a benchmark. This should NEVER take this long. assert numpy.average(timing[1:]) < 10
def test_remove_handler(self): w = Wryte(name=str(uuid.uuid4())) assert len(w.list_handlers()) == 1 assert w.list_handlers() == ['_console'] w.remove_handler('_console') assert len(w.list_handlers()) == 0 assert w.list_handlers() == []
def _test_simple_message(self): w = Wryte(color=False, simple=True) results = timeit.repeat('w.info(\'My Message\')', repeat=5, number=1000, globals={'w': w}) # This is just a benchmark. This should NEVER take this long. assert average(results) < 1
def test_simple_context(self): w = Wryte(color=False) results = timeit.repeat("w.info('My Message', {'key': 'value'})", repeat=5, number=1000, globals={'w': w}) # This is just a benchmark. This should NEVER take this long. assert average(results) < 1
def test_add_handler(self): w = Wryte(name=str(uuid.uuid4()), bare=True) assert len(w.list_handlers()) == 0 name = w.add_handler(handler=logging.StreamHandler(sys.stdout), name='_json', formatter='json', level='debug') assert len(w.list_handlers()) == 1 assert w.list_handlers() == ['_json'] assert name == '_json' assert w.get_level() == 10
def test_bind_unbind(self): w = Wryte(name=str(uuid.uuid4())) assert 'k' not in w._log.keys() w.bind({'k1': 'v1'}, '{"k2": "v2"}', k3='v3') assert 'k1' in w._log.keys() assert 'k2' in w._log.keys() assert 'k3' in w._log.keys() w.unbind('k1', 'k2', 'k3') assert 'k1' not in w._log.keys() assert 'k2' not in w._log.keys() assert 'k3' not in w._log.keys()
def _test_simple_message(count): w = Wryte(color=False, simple=False) w.remove_handler('_console') timing = [] for _ in range(5): now = datetime.now() for _ in range(count): w.info('My Message') timing.append((datetime.now() - now).total_seconds() * 1000.0) return numpy.average(timing[1:])
def test_ugly(self): w = Wryte(name=str(uuid.uuid4()), pretty=False) w.info('My Message', k='v')
def test_json(self): w = Wryte(name=str(uuid.uuid4()), jsonify=True) w.info('My Message')
def test_simple(self): w = Wryte(name=str(uuid.uuid4()), simple=True) w.info('My Message') w.event('My Event') w.debug('My Message', {'k': 'v'})
def test_aws(self): os.environ['WRYTE_EC2_ENABLED'] = 'true' w = Wryte(name=str(uuid.uuid4())) w.info('Message')
def test_logging_levels(self): w = Wryte(name=str(uuid.uuid4())) w.event('Event') w.log('info', 'My Message') w.debug('Debug Message') w.info('Info Message') w.warning('Warning Message') w.warn('Warning Message') w.error('Error Message') w.critical('Critical Message')
def test_nested_logger(self): w = Wryte(name='original') w2 = w.nested(test='test') assert 'test' not in w._log_context assert 'test' in w2._log_context
def test_remove_nonexisting_handler(self): w = Wryte(name=str(uuid.uuid4())) w.remove_handler('banana') assert len(w.list_handlers()) == 1
def test_list_handlers(self): w = Wryte(name=str(uuid.uuid4())) assert len(w.list_handlers()) == 1 assert w.list_handlers() == ['_console']
def test_list_handlers_no_handlers_configured(self): w = Wryte(name=str(uuid.uuid4()), bare=True) assert len(w.list_handlers()) == 0 assert w.list_handlers() == []
def test_another_formatter(self): w = Wryte(name=str(uuid.uuid4()), bare=True) w.add_handler(handler=logging.StreamHandler(sys.stdout), name='_json', formatter=wryte.ConsoleFormatter(), level='info')
from wryte import Wryte import timeit import statistics w = Wryte(color=False, simple=False) w.remove_handler('_console') def benchmark(code, repeat=20, number=10000): print('Benchmarking: `{}`'.format(code)) results = timeit.repeat(code, repeat=repeat, number=number, globals={'w': w}) avg = sum(results) / len(results) p90 = sorted(results)[int(len(results) * 0.9)] print('20 iterations: ') print('\n'.join(map(str, results))) print('avg: {}, P90: {}'.format(statistics.mean(results), p90)) benchmark('w.info("test message")') benchmark('w.info("test message with context", {"key": "value"})') benchmark('w.log("info", "test messge")') # without handlers, simple message # 648e0c2: ~260ms # 764dc31: ~260ms #
def test_bare_handler(self): w = Wryte(name=str(uuid.uuid4()), bare=True) assert len(w.list_handlers()) == 0
def test_no_color(self): w = Wryte(name=str(uuid.uuid4()), color=False) w.info('My Message')
def test_set_bad_level(self): w = Wryte(name=str(uuid.uuid4())) assert w.logger.getEffectiveLevel() == 20 w.set_level('deboog') assert w.logger.getEffectiveLevel() == 20
def test_context_type(self): """Test the different types of context * Dicts * Nested dicts * JSON strings * kwargs * kv strings * bad object """ w = Wryte(name=str(uuid.uuid4())) w.info('Message 1', k1='v1', k2='v2') w.info('Message 2', {'k1': 'v1', 'k2': 'v2'}) w.info('Message 3', '{"k1": "v1", "k2": "v2"}') w.info('Message 4', {'k1': 'v1', 'k2': {'k3': 'v3'}}) w.info( 'Message 5', { 'k3': 'v3', 'k4': 'v4' }, { 'k7': 'v7', 'k8': { 'k9': 'v9' } }, '{"k5": "v5", "k6": "v6"}', 'bla', k1='v1', k2='v2', )
def test_log_bad_level(self): w = Wryte(name=str(uuid.uuid4())) w.log('booboo', 'My Error')
def test_set_level_from_log(self): w = Wryte(name=str(uuid.uuid4())) assert w.get_level() == logging.INFO w.log('error', 'My Error', _set_level='debug') assert w.get_level() == logging.DEBUG
def test_set_level_from_log(self): w = Wryte(name=str(uuid.uuid4())) assert w.logger.getEffectiveLevel() == 20 w.log('error', 'My Error', _set_level='debug') assert w.logger.getEffectiveLevel() == 10
# pytype: disable=import-error from wryte import Wryte log = Wryte(name='strigo', level='info', simple=True) # pylint: disable=invalid-name
from wryte import Wryte wryter = Wryte(level='INFO') PATH = 'x' def read_config(path): wryter.info('Loading application configuration...') wryter.debug('Reading Config {0}'.format(path)) # ... raise Exception('Enabling Debug...') config_file_read = False wryter.info('Starting application...') try: config = read_config(PATH) config_file_read = True except Exception as ex: # Can also pass `set_level` to `critical`, not just to `error`. wryter.error('Failed to read config ({})'.format(ex), {'context': 'some_context'}, _set_level='debug') # do_something to reread the file, but this time with debug logging enabled. config = read_config(PATH) config_file_read = True finally: if config_file_read: