Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Merged
merged 1 commit into from
Sep 29, 2021

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Jul 23, 2021

Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #38901
License MIT
Doc PR

Usage:

# config/packages/framework.yaml
framework:
    exceptions:
        Symfony\Component\HttpKernel\Exception\BadRequestHttpException:
            log_level: debug
            status_code: 422

@lyrixx lyrixx changed the title [HttpKernel] Add support for configuration log, logLevel, and status code by exception classx [HttpKernel] Add support for configuration log, logLevel, and status code by exception class Jul 23, 2021
@lyrixx lyrixx force-pushed the log-and-exception branch from aec873b to 3d3d7b1 Compare July 23, 2021 15:47
@lyrixx lyrixx changed the title [HttpKernel] Add support for configuration log, logLevel, and status code by exception class [HttpKernel] Add support for configuring log level, and status code by exception class Jul 23, 2021
@lyrixx lyrixx force-pushed the log-and-exception branch 2 times, most recently from 06aec50 to b564507 Compare July 23, 2021 16:07
Copy link
Member

@Nyholm Nyholm left a 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..

foreach ($this->exceptionsMapping as $exception => $config) {
if ($throwable instanceof $exception && $config['status_code']) {
$response->setStatusCode($config['status_code']);
break;
Copy link
Member

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
Copy link
Member Author

lyrixx commented Jul 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?

@chalasr chalasr added this to the 5.4 milestone Aug 6, 2021
@lyrixx
Copy link
Member Author

lyrixx commented Sep 1, 2021

@Nyholm I have fixed your comments, thanks.

I think the PR is ready now

@lyrixx
Copy link
Member Author

lyrixx commented Sep 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.
If you are against it, it's time to raise your voice :)

Copy link
Member

@fabpot fabpot left a 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
Copy link
Member

fabpot commented Sep 29, 2021

Thank you @lyrixx.

@lyrixx
Copy link
Member Author

lyrixx commented Sep 29, 2021

Can you start documenting this in docs?

I'll do that ASAP :)

@lyrixx lyrixx deleted the log-and-exception branch September 29, 2021 13:56
@apfelbox
Copy link
Contributor

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 /missing/url as error, while /favicon.ico is not logged at all.

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.')
Copy link
Contributor

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

Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check see: #43399

fabpot added a commit that referenced this pull request Oct 13, 2021
…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']);
Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

@theofidry theofidry Nov 8, 2021

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).

Copy link
Member

@yceruto yceruto Nov 8, 2021

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.

@gnutix
Copy link
Contributor

gnutix commented Feb 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
Copy link
Contributor

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
Copy link
Member

yceruto commented Feb 16, 2022

@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
Copy link
Contributor

wow cool, thanks for the notif

@razvanalin
Copy link

razvanalin commented Jan 5, 2024

I'm testing a feature to log the Cars\Exceptions\CarsNotFoundException exception as an info message with a 404 status code, but it's not working as expected.

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 Cars\Exceptions\CarsNotFoundException, and the kernel.exception listener changes it to Symfony\Component\HttpKernel\Exception\HttpException. I don't want to log all HttpException instances as info, only the CarsNotFoundException

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 HttpException ?

Thank you.

@OskarStark
Copy link
Contributor

Please open a new issue with the problem and add a reference to this PR, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Let's talk about Exception and Log
Morty Proxy This is a proxified and sanitized view of the page, visit original site.