Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
06aec50 tob564507Compare
Nyholm left a comment
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 anAbstractErrorListener base class and let the defaultErrorHandler 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..
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
| foreach ($this->exceptionsMappingas$exception =>$config) { | ||
| if ($throwableinstanceof$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.
lyrixx commentedJul 26, 2021
@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? |
lyrixx commentedSep 1, 2021
@Nyholm I have fixed your comments, thanks. I think the PR is ready now |
lyrixx commentedSep 22, 2021
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. |
fabpot left a comment
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?
fabpot commentedSep 29, 2021
Thank you@lyrixx. |
lyrixx commentedSep 29, 2021
I'll do that ASAP :) |
apfelbox commentedSep 29, 2021
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 checkrange(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
| foreach ($this->exceptionsMappingas$exception =>$config) { | ||
| if ($throwableinstanceof$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 updateHtmlErrorRenderer to take this new exceptions mapping into account.
gnutix commentedFeb 16, 2022
@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. |
theofidry commentedFeb 16, 2022
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. |
yceruto commentedFeb 16, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@theofidry FYI this feature was adjusted here#44649, so your previous comment about the status code for HTML response/page should be ok now |
theofidry commentedFeb 16, 2022
wow cool, thanks for the notif |
razvanalin commentedJan 5, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I'm testing a feature to log the This is my framework exception configuration: framework:exceptions:Cars\Exceptions\CarsNotFoundException:log_level:infostatus_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 id9991057X1 {"exception":"[object] (Symfony\\Component\\HttpKernel\\Exception\\HttpException(code:0): Not found car with id9991057X1 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 id9991057X1"} [] I want to configure the exception to log asinfo with a 404 status code. Currently, my code throws framework:exceptions:Cars\Exceptions\CarsNotFoundException:log_level:infostatus_code:404Symfony\Component\HttpKernel\Exception\HttpException:log_level:infostatus_code:404 Can I achieve this without changing Thank you. |
OskarStark commentedJan 5, 2024
Please open a new issue with the problem and add a reference to this PR, thanks |
Uh oh!
There was an error while loading.Please reload this page.
Usage: