-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Add support for configuring log level, and status code by exception class #42244
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
Conversation
aec873b
to
3d3d7b1
Compare
06aec50
to
b564507
Compare
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.
Thank you for this PR.
I think @derrabus made some really good arguments in #38901 (comment)
I agree that updating logging level should be simpler.
I recognise that this is highly dependent on your app. (Think website vs API vs internal API)
Im not sure this is the best way forward. I appriciate the work and I realise it is not like me to ask for more discussion before merging a PR, but this time I would like to do that anyways.
On the top of my head (not sure if it is good or bad, I just want to start thinking on alternative solutions). What if made some better integration with an "exception handler"? Ie create an AbstractErrorListener
base class and let the default ErrorHandler
extend from it. Now we can add config for "default error listener" and use normal PHP logic to decide what do do when an exception is called.
hm..
src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php
Outdated
Show resolved
Hide resolved
foreach ($this->exceptionsMapping as $exception => $config) { | ||
if ($throwable instanceof $exception && $config['status_code']) { | ||
$response->setStatusCode($config['status_code']); | ||
break; |
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.
This means that the order you define the exceptions in your config is important. I don't mind, but this should be documented.
@Nyholm yeah, I agree with @derrabus comment. That's why I did not change the current Symfony behavior. But now, everything is configurable, that's what many people want. About a solution in pure PHP to decide or not... I'm not really convinced. The solution I propose fit my needs and is simple to use. Do you have real use case where it does not fit and you would need PHP to configure it? |
b564507
to
3b1cf07
Compare
@Nyholm I have fixed your comments, thanks. I think the PR is ready now |
3b1cf07
to
f377e39
Compare
Hello @symfony/mergers The initial issue got 8 👍🏼, 4 ❤️ and, this PR got 5 ❤️ and @dunglas commented "A big +1 on my side." on the issue. So without feedback, I'm planning to merge this PR by the end of the month, before the feature freeze. |
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.
Can you start documenting this in docs?
…y exception class
f377e39
to
94c4de3
Compare
Thank you @lyrixx. |
I'll do that ASAP :) |
It would have been nice to have system in place, to convert these exceptions in code, instead of just config. This way it might be possible to log for example a request to This way it's all or nothing. |
->info('The status code of the response. Null to let Symfony decide.') | ||
->validate() | ||
->ifTrue(function ($v) { return !\in_array($v, range(100, 499)); }) | ||
->thenInvalid('The log level is not valid. Pick one among between 100 et 599.') |
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 think the invalid message is inconsistent with the check range(100, 499)
while invalid message contains text like 100 et 599
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.
Can you please send a PR? Let's also replace this range() by two comparisons.
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.
Check see: #43399
…e to compare (JohJohan) This PR was merged into the 5.4 branch. Discussion ---------- [HttpKernel] Fix incorrect invalid message and change range to compare | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | | License | MIT | Doc PR | Fix invlid config message and change range to two comparisons #42244 (comment) Commits ------- e5a7f83 [HttpKernel] Fix incorrect invalid message and change range to compare
|
||
throw $e; | ||
} | ||
|
||
foreach ($this->exceptionsMapping as $exception => $config) { | ||
if ($throwable instanceof $exception && $config['status_code']) { | ||
$response->setStatusCode($config['status_code']); |
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.
does this work? The response is rendered L71 before this happens so whilst the response will have the correct status code the page rendered may not be matching. For example the ErrorController will render a page for a 500 although the status code will be altered here
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 don't undestant. The status code you write in the configuration is the one you want.
It is not used to "match" something. It overrides the current value.
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.
It overrides the current value
Of the created response.
In the case you are dealing with an API, you probably do not care. However, if you are dealing with an HTML response, then you probably have a different template if it's a 400 or a 500. From what I see here although you override the status code from a 500 to a 400 for example, the rendered page would still look like you had a 500 (and with the default template it shows the status code, i.e. 500).
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.
We might need to update HtmlErrorRenderer
to take this new exceptions mapping into account.
@lyrixx Is there any plan for this feature to cover errors handled in ErrorHandler ? In our app, because of huge exports of data, we may have either OutOfMemoryError or FatalError (with a message containing "Maximum execution", that represents timeouts) that we don't want to see logged with a "critical" level and have dedicated HTTP status codes (507 for OutOfMemory and 504 for timeouts). But using this mapping in the configuration for OutOfMemoryError doesn't change the criticality of the logged Fatal Error and I don't see how I could filter the FatalError only with the custom message. |
For what it's worth I did try to fix the issue found in #42244 (comment) but could not. I can't say I'm too happy about it because it means the feature really doesn't work as advertised. But given the lack of solution the best that may be done at this point is to document it. |
@theofidry FYI this feature was adjusted here #44649, so your previous comment about the status code for HTML response/page should be ok now |
wow cool, thanks for the notif |
I'm testing a feature to log the This is my framework exception configuration: framework:
exceptions:
Cars\Exceptions\CarsNotFoundException:
log_level: info
status_code: 404 However, the logger is marking it as CRITICAL: [2024-01-05T11:51:44.155556+01:00] php.CRITICAL: Uncaught Exception: Not found car with id 9991057X1 {"exception":"[object] (Symfony\\Component\\HttpKernel\\Exception\\HttpException(code: 0): Not found car with id 9991057X1 at /var/www/code/Cars/vendor/symfony/http-kernel/EventListener/ErrorListener.php:70)\n[previous exception] [object] (Cars\\Exceptions\\CarsNotFoundException(code: 404): Not found cars with id 9991057X1"} [] I want to configure the exception to log as info with a 404 status code. Currently, my code throws framework:
exceptions:
Cars\Exceptions\CarsNotFoundException:
log_level: info
status_code: 404
Symfony\Component\HttpKernel\Exception\HttpException:
log_level: info
status_code: 404 Can I achieve this without changing Thank you. |
Please open a new issue with the problem and add a reference to this PR, thanks |
Usage: