MercurialProcessor.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php declare(strict_types=1);
  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. use Monolog\Logger;
  12. /**
  13. * Injects Hg branch and Hg revision number in all records
  14. *
  15. * @author Jonathan A. Schweder <jonathanschweder@gmail.com>
  16. */
  17. class MercurialProcessor implements ProcessorInterface
  18. {
  19. private $level;
  20. private static $cache;
  21. /**
  22. * @param string|int $level The minimum logging level at which this Processor will be triggered
  23. */
  24. public function __construct($level = Logger::DEBUG)
  25. {
  26. $this->level = Logger::toMonologLevel($level);
  27. }
  28. public function __invoke(array $record): array
  29. {
  30. // return if the level is not high enough
  31. if ($record['level'] < $this->level) {
  32. return $record;
  33. }
  34. $record['extra']['hg'] = self::getMercurialInfo();
  35. return $record;
  36. }
  37. private static function getMercurialInfo(): array
  38. {
  39. if (self::$cache) {
  40. return self::$cache;
  41. }
  42. $result = explode(' ', trim(`hg id -nb`));
  43. if (count($result) >= 3) {
  44. return self::$cache = [
  45. 'branch' => $result[1],
  46. 'revision' => $result[2],
  47. ];
  48. }
  49. return self::$cache = [];
  50. }
  51. }