-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtest_logging.py
176 lines (153 loc) · 5.9 KB
/
test_logging.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import logging.config
import threading
import uuid
from unittest import mock
import pretend
import pytest
import structlog
from warehouse import logging as wlogging
class TestStructlogFormatter:
def test_warehouse_logger_no_renderer(self):
formatter = wlogging.StructlogFormatter()
record = logging.LogRecord(
"warehouse.request", logging.INFO, None, None, "the message", None, None
)
assert formatter.format(record) == "the message"
def test_non_warehouse_logger_renders(self):
formatter = wlogging.StructlogFormatter()
record = logging.LogRecord(
"another.logger", logging.INFO, None, None, "the message", None, None
)
assert json.loads(formatter.format(record)) == {
"logger": "another.logger",
"level": "INFO",
"event": "the message",
"thread": threading.get_ident(),
}
def test_create_id(monkeypatch):
uuid4 = pretend.call_recorder(lambda: "a fake uuid")
monkeypatch.setattr(uuid, "uuid4", uuid4)
request = pretend.stub()
assert wlogging._create_id(request) == "a fake uuid"
def test_create_logging(monkeypatch):
bound_logger = pretend.stub()
logger = pretend.stub(bind=pretend.call_recorder(lambda **kw: bound_logger))
monkeypatch.setattr(wlogging, "request_logger", logger)
request = pretend.stub(id="request id")
assert wlogging._create_logger(request) is bound_logger
assert logger.bind.calls == [pretend.call(**{"request.id": "request id"})]
@pytest.mark.parametrize(
("settings", "expected_level"),
[({"logging.level": "DEBUG"}, "DEBUG"), ({}, "INFO")],
)
def test_includeme(monkeypatch, settings, expected_level):
dict_config = pretend.call_recorder(lambda c: None)
monkeypatch.setattr(logging.config, "dictConfig", dict_config)
configure = pretend.call_recorder(lambda **kw: None)
monkeypatch.setattr(structlog, "configure", configure)
config = pretend.stub(
registry=pretend.stub(settings=settings),
add_request_method=pretend.call_recorder(lambda fn, name, reify: None),
)
wlogging.includeme(config)
assert dict_config.calls == [
pretend.call(
{
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"structlog": {"()": "warehouse.logging.StructlogFormatter"}
},
"handlers": {
"primary": {
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
"formatter": "structlog",
},
},
"loggers": {
"datadog.dogstatsd": {"level": "ERROR"},
"gunicorn": {
"propagate": False,
"handlers": ["primary"],
"level": expected_level,
},
"gunicorn.access": {
"propagate": False,
"handlers": ["primary"],
"level": expected_level,
},
"gunicorn.server": {
"propagate": False,
"handlers": ["primary"],
"level": expected_level,
},
"celery": {
"propagate": False,
"handlers": ["primary"],
"level": expected_level,
},
"celery.task": {
"propagate": False,
"handlers": ["primary"],
"level": expected_level,
},
"celery.worker": {
"propagate": False,
"handlers": ["primary"],
"level": expected_level,
},
"celery.app.trace": {
"propagate": False,
"handlers": ["primary"],
"level": expected_level,
},
},
"root": {"level": expected_level, "handlers": ["primary"]},
}
)
]
assert configure.calls == [
pretend.call(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
mock.ANY,
mock.ANY,
structlog.processors.format_exc_info,
wlogging.RENDERER,
],
logger_factory=mock.ANY,
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
]
assert isinstance(
configure.calls[0].kwargs["processors"][3],
structlog.stdlib.PositionalArgumentsFormatter,
)
assert isinstance(
configure.calls[0].kwargs["processors"][4],
structlog.processors.StackInfoRenderer,
)
assert isinstance(
configure.calls[0].kwargs["logger_factory"], structlog.stdlib.LoggerFactory
)
assert config.add_request_method.calls == [
pretend.call(wlogging._create_id, name="id", reify=True),
pretend.call(wlogging._create_logger, name="log", reify=True),
]