-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathtest_structlog.py
111 lines (100 loc) · 3.98 KB
/
test_structlog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from logging import Logger
from typing import Any
import pytest
import structlog
from inline_snapshot import snapshot
from logfire.integrations.structlog import LogfireProcessor
from logfire.testing import TestExporter
@pytest.fixture(autouse=True, scope='module')
def fixture_configure_structlog() -> None:
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.dev.set_exc_info,
structlog.processors.TimeStamper(fmt='%Y-%m-%d %H:%M:%S', utc=False),
LogfireProcessor(),
structlog.dev.ConsoleRenderer(),
]
)
@pytest.fixture(scope='module')
def logger() -> Any:
return structlog.get_logger()
def test_structlog(exporter: TestExporter, logger: Logger) -> None:
logger.info('This is now being logged: %s', 123)
logger.error(456)
try:
str(1 / 0)
except ZeroDivisionError:
logger.exception('error')
assert exporter.exported_spans_as_dict(fixed_line_number=None) == snapshot(
[
{
'name': 'This is now being logged: 123',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 1000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'This is now being logged: 123',
'logfire.msg': 'This is now being logged: 123',
'code.filepath': 'test_structlog.py',
'code.function': 'test_structlog',
'code.lineno': 33,
'logfire.disable_console_log': True,
},
},
{
'name': '456',
'context': {'trace_id': 2, 'span_id': 2, 'is_remote': False},
'parent': None,
'start_time': 2000000000,
'end_time': 2000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 17,
'logfire.msg_template': '456',
'logfire.msg': '456',
'code.filepath': 'test_structlog.py',
'code.function': 'test_structlog',
'code.lineno': 34,
'logfire.disable_console_log': True,
},
},
{
'name': 'error',
'context': {'trace_id': 3, 'span_id': 3, 'is_remote': False},
'parent': None,
'start_time': 3000000000,
'end_time': 3000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 17,
'logfire.msg_template': 'error',
'logfire.msg': 'error',
'code.filepath': 'test_structlog.py',
'code.function': 'test_structlog',
'code.lineno': 39,
'logfire.disable_console_log': True,
},
'events': [
{
'name': 'exception',
'timestamp': 4000000000,
'attributes': {
'exception.type': 'ZeroDivisionError',
'exception.message': 'division by zero',
'exception.stacktrace': 'ZeroDivisionError: division by zero',
'exception.escaped': 'False',
'logfire.exception_first_recorded': True,
},
}
],
},
]
)
for span in exporter.exported_spans:
assert span.instrumentation_scope.name == 'logfire.structlog' # type: ignore