Move verbosity filtering to logger (#2453)

This commit is contained in:
Adam Hopkins
2022-06-16 12:35:49 +03:00
committed by GitHub
parent b87982769f
commit 1668e1532f
7 changed files with 91 additions and 32 deletions

View File

@@ -222,6 +222,7 @@ def test_listeners_triggered_async(app, caplog):
assert after_server_stop
app.state.mode = Mode.DEBUG
app.state.verbosity = 0
with caplog.at_level(logging.DEBUG):
server.run()

View File

@@ -209,3 +209,42 @@ def test_access_log_client_ip_reqip(monkeypatch):
"request": f"GET {request.scheme}://{request.host}/",
},
)
@pytest.mark.parametrize(
"app_verbosity,log_verbosity,exists",
(
(0, 0, True),
(0, 1, False),
(0, 2, False),
(1, 0, True),
(1, 1, True),
(1, 2, False),
(2, 0, True),
(2, 1, True),
(2, 2, True),
),
)
def test_verbosity(app, caplog, app_verbosity, log_verbosity, exists):
rand_string = str(uuid.uuid4())
@app.get("/")
def log_info(request):
logger.info("DEFAULT")
logger.info(rand_string, extra={"verbosity": log_verbosity})
return text("hello")
with caplog.at_level(logging.INFO):
_ = app.test_client.get(
"/", server_kwargs={"verbosity": app_verbosity}
)
record = ("sanic.root", logging.INFO, rand_string)
if exists:
assert record in caplog.record_tuples
else:
assert record not in caplog.record_tuples
if app_verbosity == 0:
assert ("sanic.root", logging.INFO, "DEFAULT") in caplog.record_tuples