ZendMonitorHandler.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Handler;
  11. use Monolog\Formatter\FormatterInterface;
  12. use Monolog\Formatter\NormalizerFormatter;
  13. use Monolog\Logger;
  14. /**
  15. * Handler sending logs to Zend Monitor
  16. *
  17. * @author Christian Bergau <cbergau86@gmail.com>
  18. */
  19. class ZendMonitorHandler extends AbstractProcessingHandler
  20. {
  21. /**
  22. * Monolog level / ZendMonitor Custom Event priority map
  23. *
  24. * @var array
  25. */
  26. protected $levelMap = [
  27. Logger::DEBUG => 1,
  28. Logger::INFO => 2,
  29. Logger::NOTICE => 3,
  30. Logger::WARNING => 4,
  31. Logger::ERROR => 5,
  32. Logger::CRITICAL => 6,
  33. Logger::ALERT => 7,
  34. Logger::EMERGENCY => 0,
  35. ];
  36. /**
  37. * @param string|int $level The minimum logging level at which this handler will be triggered.
  38. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not.
  39. * @throws MissingExtensionException
  40. */
  41. public function __construct($level = Logger::DEBUG, bool $bubble = true)
  42. {
  43. if (!function_exists('zend_monitor_custom_event')) {
  44. throw new MissingExtensionException('You must have Zend Server installed in order to use this handler');
  45. }
  46. parent::__construct($level, $bubble);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function write(array $record): void
  52. {
  53. $this->writeZendMonitorCustomEvent(
  54. $this->levelMap[$record['level']],
  55. $record['message'],
  56. $record['formatted']
  57. );
  58. }
  59. protected function writeZendMonitorCustomEvent(int $level, string $message, array $formatted): void
  60. {
  61. zend_monitor_custom_event($level, $message, $formatted);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getDefaultFormatter(): FormatterInterface
  67. {
  68. return new NormalizerFormatter();
  69. }
  70. public function getLevelMap(): array
  71. {
  72. return $this->levelMap;
  73. }
  74. }