diff -u b/modules/redirect_404/config/schema/redirect_404.schema.yml b/modules/redirect_404/config/schema/redirect_404.schema.yml --- b/modules/redirect_404/config/schema/redirect_404.schema.yml +++ b/modules/redirect_404/config/schema/redirect_404.schema.yml @@ -14 +14 @@ - label: '404 logged messages to be suppressed.' + label: "Whether to log 'page not found' messages to the standard log or not." diff -u b/modules/redirect_404/redirect_404.module b/modules/redirect_404/redirect_404.module --- b/modules/redirect_404/redirect_404.module +++ b/modules/redirect_404/redirect_404.module @@ -53,9 +53,9 @@ $form['suppress_404'] = [ '#type' => 'checkbox', - '#title' => t('Suppress 404'), + '#title' => t("Suppress 'page not found' log messages"), '#default_value' => $config->get('suppress_404'), - '#description' => t('404 logged messages to be suppressed, which are not needed for redirect_404 to run. Enable this setting if your site gets a lot of 404 requests.'), + '#description' => t("Prevents logging 'page not found' events. Can be safely enabled when redirect_404 module is used, which stores them separately, nothing else relies on those messages."), ]; $form['#submit'][] = 'redirect_404_logging_settings_submit'; diff -u b/modules/redirect_404/redirect_404.services.yml b/modules/redirect_404/redirect_404.services.yml --- b/modules/redirect_404/redirect_404.services.yml +++ b/modules/redirect_404/redirect_404.services.yml @@ -12,5 +12,5 @@ logger.redirect_404: public: false - class: \Drupal\redirect_404\Render\Redirect404SuppressLogger + class: \Drupal\redirect_404\Render\Redirect404LogSuppressor decorates: logger.factory arguments: ['@logger.redirect_404.inner', '@config.factory'] reverted: --- b/modules/redirect_404/src/Render/Redirect404SuppressLogger.php +++ /dev/null @@ -1,63 +0,0 @@ -logger = $logger; - $this->config = $config->get('redirect_404.settings'); - } - - /** - * {@inheritdoc} - */ - public function get($channel) { - if ($this->config->get('suppress_404') && $channel == 'page not found') { - // Do not log if the suppress_404 is enabled and a 404 error is detected. - return new NullLogger(); - } - - // Call LoggerChannelFactory to let the default logger workflow proceed. - return $this->logger->get($channel); - } - - /** - * {@inheritdoc} - */ - public function addLogger(LoggerInterface $logger, $priority = 0) { - parent::addLogger($logger, $priority); - } - -} reverted: --- b/modules/redirect_404/src/Tests/Redirect404SuppressLoggerTest.php +++ /dev/null @@ -1,86 +0,0 @@ -adminUser = $this->drupalCreateUser([ - 'administer redirects', - 'administer redirect settings', - 'access site reports', - 'access content', - 'bypass node access', - 'administer site configuration', - 'access administration pages', - 'administer users', - ]); - $this->webUser = $this->drupalCreateUser([]); - } - - /** - * Tests the suppress_404 service. - */ - public function testSuppress404Events() { - // Cause a page not found and an access denied event. - $this->drupalGet('page-not-found'); - $this->assertResponse(404); - $this->drupalLogin($this->webUser); - $this->drupalGet('admin/reports/dblog'); - $this->assertResponse(403); - - // Assert the events are logged in the dblog reports. - $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type LIKE 'page not found'")->fetchField(), 1); - $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type LIKE 'access denied'")->fetchField(), 1); - - // Login as admin and enable suppress_404 to avoid logging the 404 event. - $this->drupalLogin($this->adminUser); - $edit = ['suppress_404' => TRUE]; - $this->drupalPostForm('admin/config/search/redirect/settings', $edit, 'Save configuration'); - - // Cause again a page not found and an access denied event. - $this->drupalGet('page-not-found'); - $this->assertResponse(404); - $this->drupalLogin($this->webUser); - $this->drupalGet('admin/reports/dblog'); - $this->assertResponse(403); - - // Assert only the new access denied event is logged now. - $this->drupalLogin($this->adminUser); - $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type LIKE 'page not found'")->fetchField(), 1); - $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type LIKE 'access denied'")->fetchField(), 2); - - } -} only in patch2: unchanged: --- /dev/null +++ b/modules/redirect_404/src/Render/Redirect404LogSuppressor.php @@ -0,0 +1,63 @@ +loggerChannelFactory = $logger_channel_factory; + $this->configFactory = $config_factory; + } + + /** + * {@inheritdoc} + */ + public function get($channel) { + if ($channel == 'page not found' && $this->configFactory->get('redirect_404.settings')->get('suppress_404')) { + // Do not log if a 404 error is detected and the suppress_404 is enabled. + return new NullLogger(); + } + + // Call LoggerChannelFactory to let the default logger workflow proceed. + return $this->loggerChannelFactory->get($channel); + } + + /** + * {@inheritdoc} + */ + public function addLogger(LoggerInterface $logger, $priority = 0) { + $this->loggerChannelFactory->addLogger($logger, $priority); + } + +} only in patch2: unchanged: --- /dev/null +++ b/modules/redirect_404/src/Tests/Redirect404LogSuppressorTest.php @@ -0,0 +1,80 @@ +adminUser = $this->drupalCreateUser([ + 'administer redirect settings', + 'access site reports', + ]); + $this->webUser = $this->drupalCreateUser([]); + } + + /** + * Tests the suppress_404 service. + */ + public function testSuppress404Events() { + // Cause a page not found and an access denied event. + $this->drupalGet('page-not-found'); + $this->assertResponse(404); + $this->drupalLogin($this->webUser); + $this->drupalGet('admin/reports/dblog'); + $this->assertResponse(403); + + // Assert the events are logged in the dblog reports. + $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'page not found'")->fetchField(), 1); + $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'access denied'")->fetchField(), 1); + + // Login as admin and enable suppress_404 to avoid logging the 404 event. + $this->drupalLogin($this->adminUser); + $edit = ['suppress_404' => TRUE]; + $this->drupalPostForm('admin/config/search/redirect/settings', $edit, 'Save configuration'); + + // Cause again a page not found and an access denied event. + $this->drupalGet('page-not-found'); + $this->assertResponse(404); + $this->drupalLogin($this->webUser); + $this->drupalGet('admin/reports/dblog'); + $this->assertResponse(403); + + // Assert only the new access denied event is logged now. + $this->drupalLogin($this->adminUser); + $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'page not found'")->fetchField(), 1); + $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'access denied'")->fetchField(), 2); + + } +}