-
-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(958) Add exception logging in debug mode #976
(958) Add exception logging in debug mode #976
Conversation
so, wassup here? Why does this take so long? |
not 100% sure on the testing here, can someone verify it is checking correctly? |
Co-authored-by: Na'aman Hirschfeld <[email protected]>
@JacobCoffee What's the status of this? |
The remaining problem here is that
|
I am working on a fix for the pytest issue via this |
@JacobCoffee have you seen this fixture in the logging middleware tests? I'd say that's there to solve the same problem that you are hitting here, although there might be other reasons for it also. However, if you are looking for a low friction way to get this through, then you could move that fixture out of I've done this and have tested that the full suite passes, so I'll PR it into your fork and you can take a look. If you do still want to go the route of implementing that capturing context manager pattern from the linked issue, you should perhaps look at whether we can apply that to the logging middleware tests where the current fixture-based workaround is being applied as well. |
- moves get_logger fixture to conftest.py for middleware tests - patches app logger in tests with one that propagates so caplog works.
This fixture was my first attempt as well, but it didn't seem to work as intended. @pytest.fixture
def get_logger():
return LoggingConfig(
handlers=default_handlers,
loggers={
"starlite": {"level": "DEBUG", "handlers": ["queue_listener"], "propagate": True},
},
).configure()
@pytest.mark.parametrize(
"debug,logging_config",
[
(True, LoggingConfig()),
(False, LoggingConfig()),
(False, None),
],
)
def test_exception_handler_middleware_debug_logging(
get_logger, caplog: "LogCaptureFixture", debug: bool, logging_config: Optional[LoggingConfig]
) -> None:
@get("/test")
def handler() -> None:
raise ValueError("Test debug exception")
app = Starlite([handler], logging_config=logging_config, debug=debug)
app.get_logger = get_logger
with TestClient(app=app) as client, caplog.at_level("DEBUG"):
response = client.get("/test")
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR
assert "Test debug exception" in response.text
if debug and logging_config:
assert len(caplog.records) == 1
# other assertions for the content of this record here
else:
assert not caplog.records Still fails for me. How did you get it working? Anyway, I tested the workaround @JacobCoffee linked, and it seems to me that it's a more stable solution. I think it's worth generally adopting that for our tests. Wdyt? |
I hit that too, but the code accesses |
I agree, but whether that is something that @JacobCoffee wants to do as part of this task, or we split it out to another issue, I'm not fussed. |
…ents_ps Use `get_logger` fixture to test exception logged in debug. Co-authored-by: Peter Schutt <[email protected]>
@@ -47,6 +47,8 @@ async def __call__(self, scope: "Scope", receive: "Receive", send: "Send") -> No | |||
await self.app(scope, receive, send) | |||
except Exception as e: # pylint: disable=broad-except | |||
starlite_app = scope["app"] | |||
if self.debug and (logger := starlite_app.logger): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if self.debug and (logger := starlite_app.logger): | |
if self.debug and (logger := starlite_app.get_logge()): |
no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, but it broke my test 🙃 looking into why tonight hopefully
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe because the function isn’t called get_logge
but get_logger
? 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i fixed that as soon as i committed the suggestion and realized 😛
commit dffccbf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this change breaks the test, it most likely is because we are patching app.logger
with response of calling the get_logger
fixture in the test, whereas for this to work, we'd have to patch the app.get_logger
attribute with the get_logger fixture itself.
In the starlite constructor, we both assign the get_logger callable and a logger instance to the application instance:
if self.logging_config:
self.get_logger = self.logging_config.configure()
self.logger = self.get_logger("starlite")
...and it wasn't obvious to me which one we should be using in this case either.
@all-contributors add @JacobCoffee code |
@JacobCoffee already contributed before to code |
@Goldziher Why was this merged? Tests are not passing. |
This reverts commit 21f4a2f.
Re: Issue #958
PR Checklist
CONTRIBUTING.md
?