Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:5.4fromlyrixx:log-and-exception
Sep 29, 2021

Conversation

@lyrixx
Copy link
Member

@lyrixxlyrixx commentedJul 23, 2021
edited
Loading

QA
Branch?5.4
Bug fix?no
New feature?yes
Deprecations?no
TicketsFix#38901
LicenseMIT
Doc PR

Usage:

# config/packages/framework.yamlframework:exceptions:Symfony\Component\HttpKernel\Exception\BadRequestHttpException:log_level:debugstatus_code:422

ro0NL, Nyholm, OskarStark, snoob, BafS, pyrech, bastnic, ternel, yceruto, lamasfoker, and 13 more reacted with heart emoji
@lyrixxlyrixx 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 classJul 23, 2021
@lyrixxlyrixx 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 classJul 23, 2021
@lyrixxlyrixxforce-pushed thelog-and-exception branch 2 times, most recently from06aec50 tob564507CompareJuly 23, 2021 16:07
Copy link
Member

@NyholmNyholm 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 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..

foreach ($this->exceptionsMappingas$exception =>$config) {
if ($throwableinstanceof$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 and gmponos reacted with thumbs up emoji
@lyrixx
Copy link
MemberAuthor

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

@chalasrchalasr added this to the5.4 milestoneAug 6, 2021
@lyrixx
Copy link
MemberAuthor

@Nyholm I have fixed your comments, thanks.

I think the PR is ready now

@lyrixx
Copy link
MemberAuthor

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

@fabpotfabpot 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

Thank you@lyrixx.

@lyrixx
Copy link
MemberAuthor

Can you start documenting this in docs?

I'll do that ASAP :)

@lyrixxlyrixx deleted the log-and-exception branchSeptember 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 aserror, while/favicon.ico is not logged at all.

This way it's all or nothing.

sstok and keichinger reacted with thumbs up emoji

->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 checkrange(100, 499) while invalid message contains text like 100 et 599

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 requestOct 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

foreach ($this->exceptionsMappingas$exception =>$config) {
if ($throwableinstanceof$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
MemberAuthor

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

@theofidrytheofidryNov 8, 2021
edited
Loading

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

@ycerutoycerutoNov 8, 2021
edited
Loading

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

@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 commentedFeb 16, 2022
edited
Loading

@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 commentedJan 5, 2024
edited
Loading

I'm testing a feature to log theCars\Exceptions\CarsNotFoundException exception as aninfo 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: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 throwsCars\Exceptions\CarsNotFoundException, and thekernel.exception listener changes it toSymfony\Component\HttpKernel\Exception\HttpException. I don't want to log allHttpException instances as info, only theCarsNotFoundException

framework:exceptions:Cars\Exceptions\CarsNotFoundException:log_level:infostatus_code:404Symfony\Component\HttpKernel\Exception\HttpException:log_level:infostatus_code:404

Can I achieve this without changingHttpException ?

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 freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@NyholmNyholmNyholm left review comments

@ycerutoycerutoyceruto left review comments

@fabpotfabpotfabpot approved these changes

@chalasrchalasrAwaiting requested review from chalasr

@dunglasdunglasAwaiting requested review from dunglas

@nicolas-grekasnicolas-grekasAwaiting requested review from nicolas-grekas

+2 more reviewers

@theofidrytheofidrytheofidry left review comments

@JohJohanJohJohanJohJohan left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

5.4

Development

Successfully merging this pull request may close these issues.

Let's talk about Exception and Log

13 participants

@lyrixx@fabpot@apfelbox@gnutix@theofidry@yceruto@razvanalin@OskarStark@nicolas-grekas@Nyholm@JohJohan@chalasr@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp