def test_expire_integer(): """ expire parameter can be an integer """ tagged_logger.log("+1 second", ts=datetime.datetime(2012, 1, 1), expire=1) record = tagged_logger.get_latest() assert record.expire == pytz.utc.localize(datetime.datetime(2012, 1, 1, 0, 0, 1))
def test_context(): with tagged_logger.context(ta(user="******", ip="127.0.0.1")): tagged_logger.log("{user} is from {ip}") tagged_logger.log("random message") record = tagged_logger.get_latest(user="******") assert set(record.tags) == set(["user:foo", "ip:127.0.0.1"]) assert record.attrs == {"user": "******", "ip": "127.0.0.1"}
def test_expire_timedelta(): """ expire parameter can be a timedelta """ tagged_logger.log("+1 second", ts=datetime.datetime(2012, 1, 1), expire=datetime.timedelta(seconds=1)) record = tagged_logger.get_latest() assert record.expire == pytz.utc.localize(datetime.datetime(2012, 1, 1, 0, 0, 1))
def test_context_add_tags(): """ It is possible to manually add tags to context """ tagged_logger.add_tags('foo', 'bar') tagged_logger.log('sample message') assert len(tagged_logger.get('foo')) == 1 assert len(tagged_logger.get('bar')) == 1
def test_basic(): tagged_logger.log('foo') tagged_logger.log('bar') records = tagged_logger.get() assert len(records) == 2 # messages go in reverse time order assert str(records[0]) == 'bar' assert str(records[1]) == 'foo'
def test_manual_injection(): tagged_logger.add_tagging_attrs(ta(user="******"), ip="127.0.0.1") tagged_logger.log("{user} is from {ip}") tagged_logger.rm_tagging_attrs(ta(user="******"), ip="127.0.0.1") tagged_logger.log("random message") record = tagged_logger.get_latest(user="******") assert set(record.tags) == set(["user:foo", "ip:127.0.0.1"]) assert record.attrs == {"user": "******", "ip": "127.0.0.1"}
def test_expire_archive_func(): archive = [] def do_archive(record): archive.append(record) tagged_logger.log("foo", ts=datetime.datetime(2012, 1, 1), expire=1) tagged_logger.expire(archive_func=do_archive) assert len(archive) == 1
def test_context_attrs_dict(): """ dictionary log message can be extended with attributes """ with tagged_logger.context(remote_addr='127.0.0.1'): tagged_logger.log('foo bar') record = tagged_logger.get_latest() assert record.message == 'foo bar' assert record.attrs['remote_addr'] == '127.0.0.1' assert record.message == 'foo bar'
def test_expire_archive_func_in_configure(): archive = [] def do_archive(record): archive.append(record) tagged_logger.configure(prefix=prefix, archive_func=do_archive, **redis_kwargs) tagged_logger.log("foo", ts=datetime.datetime(2012, 1, 1), expire=1) tagged_logger.expire() assert len(archive) == 1
def test_context_rm_tags(): """ You can remove tags also """ tagged_logger.add_tags('foo', 'bar') tagged_logger.rm_tags('foo') tagged_logger.rm_tags('bar') tagged_logger.rm_tags('baz') tagged_logger.log('sample message') assert len(tagged_logger.get('foo')) == 0 assert len(tagged_logger.get('bar')) == 0
def test_expire(): tagged_logger.log("1st January", expire=datetime.datetime(2012, 1, 1)) tagged_logger.log("2nd January", expire=datetime.datetime(2012, 1, 2)) expired = tagged_logger.expire(ts=datetime.datetime(2011, 12, 30, 23, 59)) assert expired == 0 assert len(tagged_logger.get()) == 2 # nothing has expired expired = tagged_logger.expire(ts=datetime.datetime(2012, 1, 1, 23, 59)) assert expired == 1 assert len(tagged_logger.get()) == 1 # one message has expired expired = tagged_logger.expire(ts=datetime.datetime(2012, 1, 2, 23, 59)) assert expired == 1 assert len(tagged_logger.get()) == 0 # all messages have expired
def test_tags(): tagged_logger.log('random action') tagged_logger.log('foo created', tags=['foo',]) tagged_logger.log('bar created', tags=['bar',]) tagged_logger.log('foo gets bar', tags=['foo', 'bar']) all_records = tagged_logger.get() assert len(all_records) == 4 foo_records = tagged_logger.get(tag='foo') assert len(foo_records) == 2
def test_naive_ts(): tagged_logger.log('foo') tagged_logger.log('bar') tagged_logger.log('baz') min_ts = datetime.datetime.utcnow() - datetime.timedelta(seconds=60) records = tagged_logger.get(min_ts=min_ts) assert len(records) == 3
def test_get_with_max_ts(): ts = datetime.datetime(2012, 1, 1, tzinfo=pytz.utc) tagged_logger.log('1st January', ts=ts) tagged_logger.log('2nd January', ts=ts + datetime.timedelta(1)) tagged_logger.log('3rd January', ts=ts + datetime.timedelta(2)) max_ts = ts + datetime.timedelta(2) - datetime.timedelta(hours=1) records = tagged_logger.get(max_ts=max_ts) assert len(records) == 2 assert str(records[0]) == '2nd January' assert str(records[1]) == '1st January'
def test_message_stores_tags(): tagged_logger.log('foo created', tags=['foo',]) record = tagged_logger.get_latest() assert record.tags == ['foo', ]
def test_limit(): tagged_logger.log('foo') tagged_logger.log('bar') records = tagged_logger.get(limit=1) assert len(records) == 1 assert str(records[0]) == 'bar'
def test_context_tags(): with tagged_logger.context('foo', 'bar'): with tagged_logger.context('baz'): tagged_logger.log('Random message', tags=['spam', ]) for tag in ('__all__', 'foo', 'bar', 'baz', 'spam'): assert len(tagged_logger.get(tag)) == 1
def test_attrs_with_message(): tagged_logger.log('user logged in', user_name='foo') record = tagged_logger.get_latest() assert record.message == 'user logged in' assert record.attrs['user_name'] == 'foo'
def test_attrs_interpolate_message(): tagged_logger.log('{user} logged in', user='******') record = tagged_logger.get_latest() assert str(record) == 'foo logged in'
def test_extra_arguments(): tagged_logger.log('{user}', user='******', user_email='*****@*****.**') record = tagged_logger.get_latest() assert str(record) == 'foo ([email protected])'
def test_attrs(): tagged_logger.log('sample message', foo='bar') record = tagged_logger.get_latest() assert record.attrs['foo'] == 'bar'
def test_missing_arguments(): tagged_logger.log('{user} {ip}', user='******') record = tagged_logger.get_latest() assert str(record) == 'foo (undefined)'
def _log_bar(): with tagged_logger.context('bar'): tagged_logger.log('bar') time.sleep(0.2)
def _log_foo(): with tagged_logger.context('foo'): tagged_logger.log('foo') time.sleep(0.2)
def test_context_reset(): tagged_logger.add_tags('foo', 'bar') tagged_logger.reset_context() tagged_logger.log('foo bar') assert len(tagged_logger.get('foo')) == 0 assert len(tagged_logger.get('bar')) == 0
def test_implicit_timestamps(): ts = datetime.datetime(2012, 1, 1, tzinfo=pytz.utc) tagged_logger.log('random action', ts=ts) record = tagged_logger.get_latest() assert record.ts == ts
def test_context_rm_attrs(): tagged_logger.add_attrs(remote_addr='127.0.0.1') tagged_logger.rm_attrs('remote_addr', 'baz') tagged_logger.log('sample message') record = tagged_logger.get_latest() assert record.attrs == {}
def do_generate_two_records(): time.sleep(0.2) tagged_logger.log('foo') tagged_logger.log('bar')