Version 1
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / src / Render / Redirect404LogSuppressor.php
1 <?php
2
3 namespace Drupal\redirect_404\Render;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7 use Drupal\Core\Logger\LoggerChannelFactory;
8 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
9 use Psr\Log\LoggerInterface;
10 use Psr\Log\NullLogger;
11
12 /**
13  * Allows 'page not found' events to be suppressed by returning a NullLogger.
14  */
15 class Redirect404LogSuppressor implements LoggerChannelFactoryInterface {
16   use DependencySerializationTrait;
17
18   /**
19    * The logger channel factory.
20    *
21    * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
22    */
23   protected $loggerChannelFactory;
24
25   /**
26    * The configuration factory.
27    *
28    * @var \Drupal\Core\Config\ConfigFactoryInterface
29    */
30   protected $configFactory;
31
32   /**
33    * Constructs a Redirect404LogSuppressor object.
34    *
35    * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_channel_factory
36    *   The logger channel factory.
37    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
38    *   The configuration factory.
39    */
40   public function __construct(LoggerChannelFactoryInterface $logger_channel_factory, ConfigFactoryInterface $config_factory) {
41     $this->loggerChannelFactory = $logger_channel_factory;
42     $this->configFactory = $config_factory;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function get($channel) {
49     if ($channel == 'page not found' && $this->configFactory->get('redirect_404.settings')->get('suppress_404')) {
50       // Do not log if a 404 error is detected and the suppress_404 is enabled.
51       return new NullLogger();
52     }
53
54     // Call LoggerChannelFactory to let the default logger workflow proceed.
55     return $this->loggerChannelFactory->get($channel);
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function addLogger(LoggerInterface $logger, $priority = 0) {
62     $this->loggerChannelFactory->addLogger($logger, $priority);
63   }
64
65 }