Skip to content

Commit

Permalink
Make error logging configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlemke committed Sep 6, 2022
1 parent 0d434df commit 3b3c210
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Classes/RedisBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b3c210

Please sign in to comment.