This is being split off from #3035589

The issue originally came up because 406 errors weren't getting caught and handled correctly. This issue is to fix the logging aspect so that 4xx errors are no longer logged as exceptions.

How to reproduce:

wget 'http://your-testing.tld/node/1?_format=hal_json'

Check the logs and you'll see the exception logged.

CommentFileSizeAuthor
#27 3039266-27.patch3.19 KBjoshua1234511
#26 3039266-26.patch3.19 KBjoshua1234511
#26 Screenshot 2022-07-17 at 12.08.48 PM.png26.21 KBjoshua1234511
#22 interdiff_13-22.txt1.91 KBravi.shankar
#22 3039266-22.patch3.36 KBravi.shankar
#18 Before---patch.jpg196.35 KBranjith_kumar_k_u
#18 After--patch.jpg200.77 KBranjith_kumar_k_u
#17 After_patch.png15.59 KBvikashsoni
#17 Before_patch.png28.36 KBvikashsoni
#14 Screenshot 2020-07-13 at 10.41.33 PM.png46.91 KBsamiullah
#14 Screenshot 2020-07-13 at 10.40.29 PM.png24.52 KBsamiullah
#14 Screenshot 2020-07-13 at 10.39.37 PM.png72.11 KBsamiullah
#13 4xx-log-3039266-13.patch3.33 KBPrimsi
#13 4xx-log-3039266-13.interdiff.txt862 bytesPrimsi
#9 4xx-log-3039266-9.patch3.24 KBPrimsi
#9 4xx-log-3039266-9.testonly.patch1.36 KBPrimsi
#8 4xx-log-3039266-8-interdiff.txt849 bytesBerdir
#8 4xx-log-3039266-8.patch1.88 KBBerdir
#5 4xx-log-3039266-4.patch1.92 KBPrimsi
#5 4xx-log-3039266-4.interdiff.txt1.63 KBPrimsi
#2 4xx-log-3039266-2.patch2.3 KBslip
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

slip created an issue. See original summary.

slip’s picture

Status: Active » Needs review
FileSize
2.3 KB

Here's a patch to fix this.

don't think we need the specific 404/403 logging so I removed it. I'm happy to add those back if for some reason putting them into separate logging types is useful.

I kept support for the on404/on403 style method calls in case removing that would be considered breaking backwards compatibility. Doesn't hurt to have it in any case imo.

slip’s picture

Status: Needs review » Needs work

Setting to needs work. I see there are references to the 'page not found' and 'access denied' logs. I'm thinking I'll add those back in and just make a generic fallback

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Primsi’s picture

Some contrib modules rely on the logic provided by existing methods (see redirect_404)

I re-added them but kept the 4xx fallback.

Primsi’s picture

Status: Needs work » Needs review
Berdir’s picture

Issue tags: +Needs tests
Berdir’s picture

Version: 8.8.x-dev » 9.1.x-dev
Assigned: slip » Unassigned
FileSize
1.88 KB
849 bytes

Reroll for Drupal 9.

Primsi’s picture

The last submitted patch, 9: 4xx-log-3039266-9.testonly.patch, failed testing. View results

joshua1234511’s picture

Status: Needs review » Reviewed & tested by the community

Reviewed the Patch #9
https://www.drupal.org/files/issues/2020-07-09/4xx-log-3039266-9.patch
Php 7.4 and Drupal core 9.1 4xx-log-3039266-9.patch

johnwebdev’s picture

Status: Reviewed & tested by the community » Needs review
  1. +++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php
    @@ -881,4 +881,26 @@ public function testBacktrace() {
    +   * Tests that a 4xx exceptions are handled.
    

    s/that a/that

  2. +++ b/core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
    @@ -57,6 +57,21 @@ public function on404(ExceptionEvent $event) {
    +   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
    ...
    +  public function on4xx(ExceptionEvent $event) {
    

    This doesn't seem right?

  3. +++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php
    @@ -881,4 +881,26 @@ public function testBacktrace() {
    +   * Tests that a 4xx exceptions are handled.
    

    /s/that a/that

  4. +++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php
    @@ -881,4 +881,26 @@ public function testBacktrace() {
    +    $uri = $node->toUrl('canonical', [
    ...
    +    $this->drupalGet($uri);
    

    Maybe a blank line, and a comment to explain what we're doing here to trigger the exception. Doesn't necessarily need a comment, but we can at least separate the preparation for the test, and the trigger of the exception.

Primsi’s picture

Thanks for the review. 3. should be correct, given that it's a fallback method for 4xx exceptions. Or did you mean something else?

samiullah’s picture

Looks good
I tested this manually
Before Applying the patch

1. Navigate to http://localhost:8888/drupal/node/1?_format=hal_json%27. (Replace localhost with ur site base url)
2. You should see site encountered error
3. Check DBlog
4. See latet php error
5. Now apply the latest patch given in #13
6. Repeat step 1 to 3
7. In dblog u should see 4xx error

This can be moved to RTBC

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Krzysztof Domański’s picture

vikashsoni’s picture

FileSize
28.36 KB
15.59 KB

Applied #13 patch Working fine showing 4xx error in recent log

ranjith_kumar_k_u’s picture

FileSize
200.77 KB
196.35 KB

The last patch works fine.

Before patch
before patch

After patch
after patch
RTBC

ranjith_kumar_k_u’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Needs work

One cs error to fix:

FILE: ...e/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 67 | ERROR | Expected type hint "GetResponseForExceptionEvent";
    |       | found "ExceptionEvent" for $event
----------------------------------------------------------------------

quietone’s picture

I did a code review.

  1. +++ b/core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
    @@ -57,6 +57,21 @@ public function on404(ExceptionEvent $event) {
    +   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
    

    This type does not agree with the parameter declared in the function.

  2. +++ b/core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
    @@ -88,9 +103,18 @@ public function onException(ExceptionEvent $event) {
    +
    +      // If the specific status code isn't handled, we try to match the status code type
    +      // For example if there's no on406() method, this will match on4xx().
    +      $method_fallback = 'on' . substr($exception->getStatusCode(), 0, 1) . 'xx';
    +
    +      // Check for methods to handle this error.
           if (method_exists($this, $possible_method)) {
             $method = $possible_method;
           }
    +      elseif (method_exists($this, $method_fallback)) {
    +        $method = $method_fallback;
    +      }
    

    I think this can be changed so that the fallback method name is only generated when $possible_method does not exist. Also, tweaked the comment to make it read a bit better and wrap at 80 columns.

          if (method_exists($this, $possible_method)) {
            $method = $possible_method;
          }
          else {
            // If the specific status code isn't handled, try to match the status
            // code type. For example, instead of using method on406() method use
            // method on4xx().
            $method_fallback = 'on' . substr($exception->getStatusCode(), 0, 1) . 'xx';
    
            if (method_exists($this, $method_fallback)) {
              $method = $method_fallback;
            }
          }
  3. +++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php
    @@ -881,4 +881,28 @@ public function testBacktrace() {
    +   * Tests that 4xx exceptions are reported in logs correctly.
    

    Lets be clear about what this does, "Tests that a 4xx errors are logged as a '4xx error'."

  4. +++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php
    @@ -881,4 +881,28 @@ public function testBacktrace() {
    +    // 4xx exceptions that are not handled should not be logged as php errors.
    

    Change to "Create a uri that will cause a 406 error."

  5. +++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php
    @@ -881,4 +881,28 @@ public function testBacktrace() {
    +    $php = Database::getConnection()->query("SELECT * FROM {watchdog} WHERE type='php'")->fetchAll();
    

    Add a comment above this line.
    // Confirm that a php error was not logged and that a 4xx error was.

  6. +++ b/core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
    @@ -57,6 +57,21 @@ public function on404(ExceptionEvent $event) {
    +      ['%status_code' => $exception->getStatusCode(), '@message' => $exception->getMessage(), '@uri' => $request->getRequestUri()]
    

    @uri is not used. Should it be?

ravi.shankar’s picture

Here I have addressed comment #20, #21.1, #21.3, #21.4, and #21.5.
Still needs work for #21.2 and #21.6

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joshua1234511’s picture

Status: Needs work » Needs review
FileSize
26.21 KB
3.19 KB

Updated the patch from #22
- Rerolled for 9.5.x
- Worked on the pending points from #21

Result: Patch

joshua1234511’s picture

FileSize
3.19 KB

Updated test

The last submitted patch, 26: 3039266-26.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 27: 3039266-27.patch, failed testing. View results

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Needs work » Closed (duplicate)
Issue tags: +Bug Smash Initiative

I think Chi is right, that this is a duplicate of an earlier issue, #2828706: ExceptionLoggingSubscriber should not log HTTP 4XX errors using PHP logger channel.

I am closing this and moving credit.