ZendMonitorHandler.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * @author Jason Davis <happydude@jasondavis.net>
  19. *
  20. * @phpstan-import-type FormattedRecord from AbstractProcessingHandler
  21. */
  22. class ZendMonitorHandler extends AbstractProcessingHandler
  23. {
  24. /**
  25. * Monolog level / ZendMonitor Custom Event priority map
  26. *
  27. * @var array<int, int>
  28. */
  29. protected $levelMap = [];
  30. /**
  31. * @throws MissingExtensionException
  32. */
  33. public function __construct($level = Logger::DEBUG, bool $bubble = true)
  34. {
  35. if (!function_exists('zend_monitor_custom_event')) {
  36. throw new MissingExtensionException(
  37. 'You must have Zend Server installed with Zend Monitor enabled in order to use this handler'
  38. );
  39. }
  40. //zend monitor constants are not defined if zend monitor is not enabled.
  41. $this->levelMap = [
  42. Logger::DEBUG => \ZEND_MONITOR_EVENT_SEVERITY_INFO,
  43. Logger::INFO => \ZEND_MONITOR_EVENT_SEVERITY_INFO,
  44. Logger::NOTICE => \ZEND_MONITOR_EVENT_SEVERITY_INFO,
  45. Logger::WARNING => \ZEND_MONITOR_EVENT_SEVERITY_WARNING,
  46. Logger::ERROR => \ZEND_MONITOR_EVENT_SEVERITY_ERROR,
  47. Logger::CRITICAL => \ZEND_MONITOR_EVENT_SEVERITY_ERROR,
  48. Logger::ALERT => \ZEND_MONITOR_EVENT_SEVERITY_ERROR,
  49. Logger::EMERGENCY => \ZEND_MONITOR_EVENT_SEVERITY_ERROR,
  50. ];
  51. parent::__construct($level, $bubble);
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function write(array $record): void
  57. {
  58. $this->writeZendMonitorCustomEvent(
  59. Logger::getLevelName($record['level']),
  60. $record['message'],
  61. $record['formatted'],
  62. $this->levelMap[$record['level']]
  63. );
  64. }
  65. /**
  66. * Write to Zend Monitor Events
  67. * @param string $type Text displayed in "Class Name (custom)" field
  68. * @param string $message Text displayed in "Error String"
  69. * @param array $formatted Displayed in Custom Variables tab
  70. * @param int $severity Set the event severity level (-1,0,1)
  71. *
  72. * @phpstan-param FormattedRecord $formatted
  73. */
  74. protected function writeZendMonitorCustomEvent(string $type, string $message, array $formatted, int $severity): void
  75. {
  76. zend_monitor_custom_event($type, $message, $formatted, $severity);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getDefaultFormatter(): FormatterInterface
  82. {
  83. return new NormalizerFormatter();
  84. }
  85. /**
  86. * @return array<int, int>
  87. */
  88. public function getLevelMap(): array
  89. {
  90. return $this->levelMap;
  91. }
  92. }