UidProcessor.php 944 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Processor;
  11. /**
  12. * Adds a unique identifier into records
  13. *
  14. * @author Simon Mönch <sm@webfactory.de>
  15. */
  16. class UidProcessor
  17. {
  18. private $uid;
  19. public function __construct($length = 7)
  20. {
  21. if (!is_int($length) || $length > 32 || $length < 1) {
  22. throw new \InvalidArgumentException('The uid length must be an integer between 1 and 32');
  23. }
  24. $this->uid = substr(hash('md5', uniqid('', true)), 0, $length);
  25. }
  26. public function __invoke(array $record)
  27. {
  28. $record['extra']['uid'] = $this->uid;
  29. return $record;
  30. }
  31. /**
  32. * @return string
  33. */
  34. public function getUid()
  35. {
  36. return $this->uid;
  37. }
  38. }