تخطَّ إلى المحتوى

Log And Rethrow Duplication (ARCH7166)

هذا المحتوى غير متوفر بلغتك بعد.

Key php.logging.log-and-rethrow · Severity warning · Category logging

Logging an exception and rethrowing it in the same handler produces duplicate log entries, since it is logged again higher up the stack.

<?php
try {
$this->repository->save($entity);
} catch (\PDOException $e) {
$this->logger->error('Save failed', ['exception' => $e]);
throw $e;
}
<?php
try {
$this->repository->save($entity);
} catch (\PDOException $e) {
throw new PersistenceException('Save failed', 0, $e);
}

Either log and swallow, or add context and rethrow - not both.