MercurialProcessor.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jonathan A. Schweder <jonathanschweder@gmail.com>
  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
  18. {
  19. private $level;
  20. private static $cache;
  21. public function __construct($level = Logger::DEBUG)
  22. {
  23. $this->level = Logger::toMonologLevel($level);
  24. }
  25. /**
  26. * @param array $record
  27. * @return array
  28. */
  29. public function __invoke(array $record)
  30. {
  31. // return if the level is not high enough
  32. if ($record['level'] < $this->level) {
  33. return $record;
  34. }
  35. $record['extra']['hg'] = self::getMercurialInfo();
  36. return $record;
  37. }
  38. private static function getMercurialInfo()
  39. {
  40. if (self::$cache) {
  41. return self::$cache;
  42. }
  43. $result = explode(' ', trim(`hg id -nb`));
  44. if (count($result) >= 3) {
  45. return self::$cache = array(
  46. 'branch' => $result[1],
  47. 'revision' => $result[2],
  48. );
  49. }
  50. return self::$cache = array();
  51. }
  52. }