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_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_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_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_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_context_multithread(): foo_thread = threading.Thread(target=_log_foo) bar_thread = threading.Thread(target=_log_bar) foo_thread.start() time.sleep(0.1) bar_thread.start() foo_thread.join() bar_thread.join() # check results foo_records = tagged_logger.get('foo') assert len(foo_records) == 1 assert str(foo_records[0]) == 'foo' bar_records = tagged_logger.get('bar') assert len(bar_records) == 1 assert str(bar_records[0]) == 'bar'
def do_get(): logger.configure(prefix=options.prefix) min_ts = options.min_ts and parser.parse(options.min_ts) max_ts = options.max_ts and parser.parse(options.max_ts) records = logger.get(tag=options.tag, limit=options.limit, min_ts=min_ts, max_ts=max_ts) for record in records: ts = record.ts.strftime(options.time_format) formatted = '{0} {1}'.format(ts, str(record)) print(formatted)
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_full_cleanup(): assert tagged_logger.get() == []
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_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_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