mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-22 17:16:18 +02:00
Added a new ResettableInterface and implemented it where possible.
When one use Monolog in a long process like an AMQP worker with a `FingersCrossedHandler` or `BufferHandler` there is a drawback: as soon as there is an AMQP message that generate a log >= error (for example), all next AMQP messages will output logs, even if theses messages don't generate log where level >= error. In the same context there is a drawback for processor that add an UUID to the logs. The UUID should change for each AMQP messages. --- This patch address this issue with a new interface: `ResettableInterface` interface. Side note: `reset()`, `flush()`, `clear()`, are already used in Monolog. So basically, one can use the `reset()` on the `Logger` and on some `Handler`s / `Processor`s. It's especially useful for * the `FingersCrossedHandler`: it `close()` the buffer, then it `clear()` the buffer. * the `BufferHandler`: it `flush()` the buffer, then it `clear()` the buffer. * the `UidProcessor`: it renew the `uid`.
This commit is contained in:
@@ -11,12 +11,14 @@
|
||||
|
||||
namespace Monolog\Processor;
|
||||
|
||||
use Monolog\ResettableInterface;
|
||||
|
||||
/**
|
||||
* Adds a unique identifier into records
|
||||
*
|
||||
* @author Simon Mönch <sm@webfactory.de>
|
||||
*/
|
||||
class UidProcessor
|
||||
class UidProcessor implements ResettableInterface
|
||||
{
|
||||
private $uid;
|
||||
|
||||
@@ -26,7 +28,8 @@ class UidProcessor
|
||||
throw new \InvalidArgumentException('The uid length must be an integer between 1 and 32');
|
||||
}
|
||||
|
||||
$this->uid = substr(hash('md5', uniqid('', true)), 0, $length);
|
||||
|
||||
$this->uid = $this->generateUid($length);
|
||||
}
|
||||
|
||||
public function __invoke(array $record)
|
||||
@@ -43,4 +46,14 @@ class UidProcessor
|
||||
{
|
||||
return $this->uid;
|
||||
}
|
||||
|
||||
public function reset()
|
||||
{
|
||||
$this->uid = $this->generateUid(strlen($this->uid));
|
||||
}
|
||||
|
||||
private function generateUid($length)
|
||||
{
|
||||
return substr(hash('md5', uniqid('', true)), 0, $length);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user