8000 feautre: add the ability to modify the logger level in lock component · symfony/symfony@1c64a3d · GitHub
[go: up one dir, main page]

Skip to content

Commit 1c64a3d

Browse files
author
Amrouche Hamza
committed
feautre: add the ability to modify the logger level in lock component
1 parent db10ee7 commit 1c64a3d

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/Symfony/Component/Lock/Lock.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,22 @@ final class Lock implements LockInterface, LoggerAwareInterface
3434
private $ttl;
3535
private $autoRelease;
3636
private $dirty = false;
37+
private $logLevel;
3738

3839
/**
3940
* @param Key $key Resource to lock
4041
* @param StoreInterface $store Store used to handle lock persistence
4142
* @param float|null $ttl Maximum expected lock duration in seconds
4243
* @param bool $autoRelease Whether to automatically release the lock or not when the lock instance is destroyed
44+
* @param string|null $logLevel The level of log you want for errors e.g 'warning', 'info'.
4345
*/
44-
public function __construct(Key $key, StoreInterface $store, float $ttl = null, bool $autoRelease = true)
46+
public function __construct(Key $key, StoreInterface $store, float $ttl = null, bool $autoRelease = true, string $logLevel = null)
4547
{
4648
$this->store = $store;
4749
$this->key = $key;
4850
$this->ttl = $ttl;
4951
$this->autoRelease = $autoRelease;
52+
$this->logLevel = $logLevel;
5053

5154
$this->logger = new NullLogger();
5255
}
@@ -89,15 +92,15 @@ public function acquire($blocking = false)
8992
return true;
9093
} catch (LockConflictedException $e) {
9194
$this->dirty = false;
92-
$this->logger->warning('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', array('resource' => $this->key));
95+
$this->logger->log($this->logLevel ?? 'warning', 'Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', array('resource< 8000 /span>' => $this->key));
9396

9497
if ($blocking) {
9598
throw $e;
9699
}
97100

98101
return false;
99102
} catch (\Exception $e) {
100-
$this->logger->warning('Failed to acquire the "{resource}" lock.', array('resource' => $this->key, 'exception' => $e));
103+
$this->logger->log($this->logLevel ?? 'warning', 'Failed to acquire the "{resource}" lock.', array('resource' => $this->key, 'exception' => $e));
101104
throw new LockAcquiringException(sprintf('Failed to acquire the "%s" lock.', $this->key), 0, $e);
102105
}
103106
}
@@ -123,10 +126,10 @@ public function refresh()
123126
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $this->ttl));
124127
} catch (LockConflictedException $e) {
125128
$this->dirty = false;
126-
$this->logger->warning('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', array('resource' => $this->key));
129+
$this->logger->log($this->logLevel ?? 'warning', 'Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', array('resource' => $this->key));
127130
throw $e;
128131
} catch (\Exception $e) {
129-
$this->logger->warning('Failed to define an expiration for the "{resource}" lock.', array('resource' => $this->key, 'exception' => $e));
132+
$this->logger->log($this->logLevel ?? 'warning', 'Failed to define an expiration for the "{resource}" lock.', array('resource' => $this->key, 'exception' => $e));
130133
throw new LockAcquiringException(sprintf('Failed to define an expiration for the "%s" lock.', $this->key), 0, $e);
131134
}
132135
}
@@ -148,7 +151,7 @@ public function release()
148151
$this->dirty = false;
149152

150153
if ($this->store->exists($this->key)) {
151-
$this->logger->warning('Failed to release the "{resource}" lock.', array('resource' => $this->key));
154+
$this->logger->log($this->logLevel ?? 'warning', 'Failed to release the "{resource}" lock.', array('resource' => $this->key));
152155
throw new LockReleasingException(sprintf('Failed to release the "%s" lock.', $this->key));
153156
}
154157
}

src/Symfony/Component/Lock/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\LoggerInterface;
1516
use Symfony\Component\Lock\Exception\LockConflictedException;
1617
use Symfony\Component\Lock\Key;
1718
use Symfony\Component\Lock\Lock;
@@ -192,6 +193,35 @@ public function testReleaseThrowsExceptionIfNotWellDeleted()
192193
$lock->release();
193194
}
194195

196+
/**
197+
* @expectedException \Symfony\Component\Lock\Exception\LockReleasingException
198+
*/
199+
public function testReleaseThrowsAndLog()
200+
{
201+
$key = new Key(uniqid(__METHOD__, true));
202+
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
203+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
204+
$lock = new Lock($key, $store, 10, true, 'info');
205+
$lock->setLogger($logger);
206+
207+
$logger->expects($this->atLeastOnce())
208+
->method('log')
209+
->with('info', 'Failed to release the "{resource}" lock.', array('resource' => $key));
210+
211+
$store
212+
->expects($this->once())
213+
->method('delete')
214+
->with($key);
215+
216+
$store
217+
->expects($this->once())
218+
->method('exists')
219+
->with($key)
220+
->willReturn(true);
221+
222+
$lock->release();
223+
}
224+
195225
/**
196226
* @dataProvider provideExpiredDates
197227
*/

0 commit comments

Comments
 (0)
0