def logging(): """Setup default logging for tests. Tests can reconfigure logging if they wish to. """ configure_logging( log_format='kv', utc=False, endpoint='file:///dev/stdout', )
def test_log_format(log_format, expected): with freeze_time("2016-05-08 21:19:00"): with testfixtures.OutputCapture() as capture: configure_logging( log_format=log_format, utc=False, endpoint='file:///dev/stderr', ) log = structlog.get_logger() log.info('teh.event', a=1, b=2) capture.compare(expected)
def test_configure_logging_unknown_scheme(capsys, tempdir): with pytest.raises(ValueError) as error: configure_logging( log_format='kv', utc=False, endpoint='flume://127.0.0.1:44444', ) log = structlog.get_logger() log.info('teh.event', a=1) assert str(error.value) == \ 'Invalid logging endpoint "flume://127.0.0.1:44444".'
def test_configure_logging_stderr(capsys): with freeze_time("2016-05-08 21:19:00"): configure_logging( log_format='kv', utc=False, endpoint='file:///dev/stderr', ) log = structlog.get_logger() log.info('teh.event', a=1) out, err = capsys.readouterr() assert out == "" assert err == "@timestamp='2016-05-08T21:19:00' event='teh.event' a=1\n"
def test_configure_logging_file(capsys, tempdir): with freeze_time("2016-05-08 21:19:00"): configure_logging( log_format='kv', utc=False, endpoint='file://./gitmesh.log', ) log = structlog.get_logger() log.info('teh.event', a=1) out, err = capsys.readouterr() assert out == "" assert err == "" with open('./gitmesh.log', 'r') as stream: logs = stream.read() assert logs == "@timestamp='2016-05-08T21:19:00' event='teh.event' a=1\n"
def test_logging_fluentd(emit, logging_endpoint, utc, expected_timestamp): with freeze_time("2016-05-08 21:19:00"): configure_logging( log_format='kv', # Ignored! utc=utc, endpoint=logging_endpoint, ) log = structlog.get_logger() with testfixtures.OutputCapture() as capture: log.info('teh.event', a=1, b=2) capture.compare('') emit.assert_called_once_with('teh.event', { 'a': 1, 'b': 2, '@timestamp': expected_timestamp, })
def test_logging_fluentd_override_timestamp(emit, timestamp, expected_timestamp): with freeze_time("2016-05-08 21:19:00"): configure_logging( log_format='kv', # Ignored! utc=False, endpoint='fluent://127.0.0.1:24224/the-app', ) log = structlog.get_logger() with testfixtures.OutputCapture() as capture: log.info('teh.event', a=1, b=2, **{'@timestamp': timestamp}) capture.compare('') emit.assert_called_once_with('teh.event', { 'a': 1, 'b': 2, '@timestamp': expected_timestamp, })