Skip to content

Commit 94dfe5f

Browse files
PabloKowalczykfabpot
authored andcommitted
Log successful release of Lock
1 parent 90d7c9c commit 94dfe5f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Lock.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ public function release()
236236
if ($this->store->exists($this->key)) {
237237
throw new LockReleasingException(sprintf('Failed to release the "%s" lock, the resource is still locked.', $this->key));
238238
}
239+
240+
$this->logger->debug('Successfully released the "{resource}" lock.', ['resource' => $this->key]);
239241
} catch (LockReleasingException $e) {
240242
$this->logger->notice('Failed to release the "{resource}" lock.', ['resource' => $this->key]);
241243
throw $e;

Tests/LockTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Lock\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\AbstractLogger;
1516
use Psr\Log\LoggerInterface;
1617
use Symfony\Component\Lock\BlockingSharedLockStoreInterface;
1718
use Symfony\Component\Lock\BlockingStoreInterface;
@@ -22,6 +23,7 @@
2223
use Symfony\Component\Lock\PersistingStoreInterface;
2324
use Symfony\Component\Lock\SharedLockStoreInterface;
2425
use Symfony\Component\Lock\Store\ExpiringStoreTrait;
26+
use Symfony\Component\Lock\Store\InMemoryStore;
2527

2628
/**
2729
* @author Jérémy Derussé <jeremy@derusse.com>
@@ -366,6 +368,34 @@ public function testReleaseThrowsAndLog()
366368
$lock->release();
367369
}
368370

371+
public function testSuccessReleaseLog()
372+
{
373+
$key = new Key((string) random_int(100, 1000));
374+
$store = new InMemoryStore();
375+
$logger = new class() extends AbstractLogger {
376+
private array $logs = [];
377+
378+
public function log($level, $message, array $context = []): void
379+
{
380+
$this->logs[] = [
381+
$level,
382+
(string) $message,
383+
$context,
384+
];
385+
}
386+
387+
public function logs(): array
388+
{
389+
return $this->logs;
390+
}
391+
};
392+
$lock = new Lock($key, $store, 10, true);
393+
$lock->setLogger($logger);
394+
$lock->release();
395+
396+
$this->assertSame([['debug', 'Successfully released the "{resource}" lock.', ['resource' => $key]]], $logger->logs());
397+
}
398+
369399
/**
370400
* @dataProvider provideExpiredDates
371401
*/

0 commit comments

Comments
 (0)