-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Monolog Bridge][DX] Add a Monolog activation strategy for ignoring specific HTTP codes #23707
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
[Monolog Bridge][DX] Add a Monolog activation strategy for ignoring specific HTTP codes #23707
Conversation
New features should target the 3.4 branch. |
9a93318
to
7beede2
Compare
Sorry about that. Rebased to 3.4. How do I get AppVeyor to run again? It says "continuous-integration/appveyor/pr — AppVeyor was unable to build non-mergeable " |
Seems like you changed the PR target but not correctly rebased to the 3.4 branch. |
33e4c94
to
6a0e3e6
Compare
I think I fixed the rebase. The Travis failure seems to be unrelated:
|
Just wondering: couldn't/shouldn't this be submitted to monolog instead? |
Maybe. I did it here since the bridge contains the |
OK my bad it's legit, Symfony namespace here :) |
So, ping @lsmith77 I guess, original report of the feature request. |
Looks useful to me, but what about deprecating the |
$isActivated | ||
&& isset($record['context']['exception']) | ||
&& $record['context']['exception'] instanceof HttpException | ||
&& ($request = $this->requestStack->getMasterRequest()) |
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.
$request
is not used
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 you are wrong, $request
is used here: https://github.com/symfony/symfony/pull/23707/files#diff-ca46116b05ee5c3e47d1438e580c53eeR53
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 wasn't being used before. It is now with the addition of URL blacklisting.
Moving to 4.1. Rebase on master might be needed, where PHP 7.1 features can be used btw. |
* | ||
* @author Shaun Simmons <[email protected]> | ||
*/ | ||
class HttpCodeActivationStrategy extends ErrorLevelActivationStrategy |
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 should not extend ErrorLevelActivationStrategy
but rather implement ActivationStrategyInterface
, since this strategy doesn't deal with the error levels.
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.
Plus, this class could be final
IMO.
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.
@pimolo The FingersCrossedHandler leaves it up to the activation strategy object to handle the action level. It defaults to the ErrorLevelActivationStrategy only when it isn't explicitly given an object implementing ActivationStrategyInterface. Because an HttpCodeActivationStrategy object is given to the FingersCrossedHandler when the excluded_http_codes
configuration exists, the HttpCodeActivationStrategy becomes responsible for handling the action level, just like the existing NotFoundActivationStrategy does. So, that's why it's extending the ErrorLevelActivationStrategy class.
* | ||
* @author Shaun Simmons <[email protected]> | ||
*/ | ||
class HttpCodeActivationStrategy extends ErrorLevelActivationStrategy |
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.
Plus, this class could be final
IMO.
@nicolas-grekas What is the status of the PR? @simshaun are you sill handling it? If not, I may rework it.
@fabpot Makes sense to me. We could imagine something like that: excluded_http_erros:
403: ^/ # All 403 errors are excluded
404: ^/admin # All 404 errors are excluded for /admin/*
400: # Can be still multiple regex.
- ^/csrf/
- ^/foo/bar/ |
I'm working on adding the URL blacklist. @fabpot, does the configuration format @soullivaneuh showed above look good? One thing I'm thinking could be improved for user-friendliness: If you want a code to be excluded for all URLs, you just pass excluded_http_codes:
403: true # All 403 errors are excluded
404: ^/admin # All 404 errors are excluded for /admin/*
400: # Can contain multiple regex
- ^/csrf/
- ^/foo/bar/ |
@simshaun I was thinking about the |
Just asking: do we really want to allow excluding HTTP status per URL? If we don't do that, this would be really simple: excluded_http_codes: [400, 403, 404] If we really must support excluding per URL, do we really want to mix booleans and regexp strings? As proposed above, putting And now a proposal that mixes both ideas. What if we add support for URLs, but make them optional? Like this: excluded_http_codes: [403, 404]
excluded_http_codes: [{ 404: '^/some/section' }]
excluded_http_codes: [403, 404, { 400: ['^/csrf', '^/foo/bar' ] }] |
I'm good with that too. |
I've updated this PR to support URL blacklists using the YAML format suggested by @javiereguiluz. The other PRs referenced in the OP (MonologBundle and docs) have been updated as well. I really struggled with making the Configuration tree work with both YAML and XML. I ended up just normalizing the two into a common format that the new activation strategy understands. It now supports: YAML: excluded_http_codes: [403, 404, { 400: ['^/foo', '^/bar'] }] XML: <monolog:excluded-http-code code="403" />
<monolog:excluded-http-code code="404" />
<monolog:excluded-http-code code="400">
<monolog:url>^/foo</monolog:url>
<monolog:url>^/bar</monolog:url>
</monolog:excluded-http-code> |
Looks like the Travis failure is unrelated. Lots of problems with |
} | ||
|
||
$urlBlacklist = null; | ||
if (count($exclusion['url'])) { |
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 ! empty(...) could prevent an issue when the url index is not defined.
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.
Validation added in the constructor
&& ($request = $this->requestStack->getMasterRequest()) | ||
) { | ||
foreach ($this->exclusions as $exclusion) { | ||
if ($record['context']['exception']->getStatusCode() !== $exclusion['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.
Would it be useful to ensure that the code key exists, and throw otherwise?
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.
Done in the constructor
f108940
to
067502d
Compare
be3ec97
to
27f9cb2
Compare
27f9cb2
to
6fc1cc3
Compare
Thank you @simshaun. |
… for ignoring specific HTTP codes (simshaun, fabpot) This PR was merged into the 4.1-dev branch. Discussion ---------- [Monolog Bridge][DX] Add a Monolog activation strategy for ignoring specific HTTP codes | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #9712 | License | MIT | Doc PR | symfony/symfony-docs#8235 This PR introduces a Monolog activation strategy that makes it easy to ignore specific HTTP codes. e.g. Stopping logs from being flooded with 403s, 404s, and 405s. Relevant Symfony integration PR on symfony/monolog-bundle: symfony/monolog-bundle#221 Commits ------- 6fc1cc3 added some validation c11a8eb Add a Monolog activation strategy for ignoring specific HTTP codes
…mshaun, fabpot) This PR was merged into the 3.x-dev branch. Discussion ---------- Add configuration for new HttpCodeActivationStrategy Dependent upon symfony/symfony#23707 This PR introduces configuration to integrate a new `HttpCodeActivationStrategy`, offering an easy way to ignore specific HTTP status codes. Commits ------- 30a70a2 tweaked code 61a78a6 Add configuration for new HttpCodeActivationStrategy
…n, javiereguiluz) This PR was merged into the 4.1 branch. Discussion ---------- Document new Monolog HTTP code exclusion feature I'm submitting PRs on symfony/symfony and symfony/monolog-bundle that introduces an easy way of excluding specific HTTP codes from Monolog. This PR documents that new feature, pending its acceptance. Pending PRs: symfony/symfony#23707 symfony/monolog-bundle#221 Commits ------- 30cc7d4 Added the versionadded directive c8c19f2 Update to Symfony 4 paths e19bca2 Fix indentation 17cb416 Update configuration format to support URL blacklist c9f6ee5 Fix title underline length a3b6a01 Document new Monolog HTTP code exclusion feature
The feature is not working as expected. I added and I still got:
|
...should I open a new issue? |
@fmonts A new issue would be better. Please @ me and I'll try to take a look. Also, please include the context of your logging config. (I'm mostly just curious if you're using the fingers_crossed type.) |
…mshaun, fabpot) This PR was merged into the 3.x-dev branch. Discussion ---------- Add configuration for new HttpCodeActivationStrategy Dependent upon symfony/symfony#23707 This PR introduces configuration to integrate a new `HttpCodeActivationStrategy`, offering an easy way to ignore specific HTTP status codes. Commits ------- 2e7b3fe tweaked code f90e3e6 Add configuration for new HttpCodeActivationStrategy
This PR introduces a Monolog activation strategy that makes it easy to ignore specific HTTP codes. e.g. Stopping logs from being flooded with 403s, 404s, and 405s.
Relevant Symfony integration PR on symfony/monolog-bundle: symfony/monolog-bundle#221