From 3b3c21012c6cc5776aa103fc6e476907c87344aa Mon Sep 17 00:00:00 2001 From: Robert Lemke Date: Tue, 6 Sep 2022 14:05:13 +0200 Subject: [PATCH] Make error logging configurable --- Classes/RedisBackend.php | 15 +++++++++++++-- README.md | 12 ++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Classes/RedisBackend.php b/Classes/RedisBackend.php index 92c9755..e07e7db 100644 --- a/Classes/RedisBackend.php +++ b/Classes/RedisBackend.php @@ -53,6 +53,7 @@ class RedisBackend extends IndependentAbstractBackend implements TaggableBackend protected ?Iterator\Keyspace $entryKeyspaceIterator = null; protected int $entryKeyspaceIteratorKeyPrefixLength = 0; protected bool $deduplicateErrors = true; + protected bool $logErrors = true; protected static array $loggedErrors = []; @@ -65,7 +66,7 @@ public function __construct(EnvironmentConfiguration $environmentConfiguration, parent::__construct($environmentConfiguration, $options); $this->client = $this->getRedisClient(); - if (class_exists('Neos\Flow\Core\Bootstrap') && Bootstrap::$staticObjectManager instanceof ObjectManagerInterface) { + if ($this->logErrors && class_exists(Bootstrap::class) && Bootstrap::$staticObjectManager instanceof ObjectManagerInterface) { $this->logger = Bootstrap::$staticObjectManager->get(LoggerInterface::class); $this->throwableStorage = Bootstrap::$staticObjectManager->get(ThrowableStorageInterface::class); } @@ -497,6 +498,14 @@ public function setDeduplicateErrors($deduplicateErrors): void $this->deduplicateErrors = (bool)$deduplicateErrors; } + /** + * @param bool $logErrors + */ + public function setLogErrors(bool $logErrors): void + { + $this->logErrors = $logErrors; + } + /** * @param string|bool $value * @return string|bool @@ -583,7 +592,9 @@ private function handleThrowable(\Throwable $throwable): void { $messageHash = md5($throwable->getMessage()); if (!$this->deduplicateErrors || !array_key_exists($messageHash, static::$loggedErrors)) { - $this->logger && $this->logger->error($this->throwableStorage->logThrowable($throwable), LogEnvironment::fromMethodName(__METHOD__)); + if ($this->logErrors && $this->logger && $this->throwableStorage) { + $this->logger && $this->logger->error($this->throwableStorage->logThrowable($throwable), LogEnvironment::fromMethodName(__METHOD__)); + } static::$loggedErrors[$messageHash] = true; } throw $throwable; diff --git a/README.md b/README.md index 9da514e..aec9b35 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,18 @@ Flow_Mvc_Routing_Route: deduplicateErrors: false ``` +If you don't want errors being logged – for example, because you log errors via +the MultiBackend – you can turn off logging for this cache backend: + +```yaml +Flow_Mvc_Routing_Route: + backend: 'Flownative\RedisSentinel\RedisBackend' + backendOptions: + database: 0 + … + logErrors: false +``` + ## Tests You can adjust the host, port and password used in the functional tests