3 namespace Drupal\redirect_404\Render;
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;
13 * Allows 'page not found' events to be suppressed by returning a NullLogger.
15 class Redirect404LogSuppressor implements LoggerChannelFactoryInterface {
16 use DependencySerializationTrait;
19 * The logger channel factory.
21 * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
23 protected $loggerChannelFactory;
26 * The configuration factory.
28 * @var \Drupal\Core\Config\ConfigFactoryInterface
30 protected $configFactory;
33 * Constructs a Redirect404LogSuppressor object.
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.
40 public function __construct(LoggerChannelFactoryInterface $logger_channel_factory, ConfigFactoryInterface $config_factory) {
41 $this->loggerChannelFactory = $logger_channel_factory;
42 $this->configFactory = $config_factory;
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();
54 // Call LoggerChannelFactory to let the default logger workflow proceed.
55 return $this->loggerChannelFactory->get($channel);
61 public function addLogger(LoggerInterface $logger, $priority = 0) {
62 $this->loggerChannelFactory->addLogger($logger, $priority);