1<?php declare(strict_types=1);
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog\Handler;
13
14use Monolog\Formatter\FormatterInterface;
15use Monolog\Formatter\NormalizerFormatter;
16use Monolog\Logger;
17
18/**
19 * Handler sending logs to Zend Monitor
20 *
21 * @author  Christian Bergau <cbergau86@gmail.com>
22 * @author  Jason Davis <happydude@jasondavis.net>
23 *
24 * @phpstan-import-type FormattedRecord from AbstractProcessingHandler
25 */
26class ZendMonitorHandler extends AbstractProcessingHandler
27{
28    /**
29     * Monolog level / ZendMonitor Custom Event priority map
30     *
31     * @var array<int, int>
32     */
33    protected $levelMap = [];
34
35    /**
36     * @throws MissingExtensionException
37     */
38    public function __construct($level = Logger::DEBUG, bool $bubble = true)
39    {
40        if (!function_exists('zend_monitor_custom_event')) {
41            throw new MissingExtensionException(
42                'You must have Zend Server installed with Zend Monitor enabled in order to use this handler'
43            );
44        }
45        //zend monitor constants are not defined if zend monitor is not enabled.
46        $this->levelMap = [
47            Logger::DEBUG     => \ZEND_MONITOR_EVENT_SEVERITY_INFO,
48            Logger::INFO      => \ZEND_MONITOR_EVENT_SEVERITY_INFO,
49            Logger::NOTICE    => \ZEND_MONITOR_EVENT_SEVERITY_INFO,
50            Logger::WARNING   => \ZEND_MONITOR_EVENT_SEVERITY_WARNING,
51            Logger::ERROR     => \ZEND_MONITOR_EVENT_SEVERITY_ERROR,
52            Logger::CRITICAL  => \ZEND_MONITOR_EVENT_SEVERITY_ERROR,
53            Logger::ALERT     => \ZEND_MONITOR_EVENT_SEVERITY_ERROR,
54            Logger::EMERGENCY => \ZEND_MONITOR_EVENT_SEVERITY_ERROR,
55        ];
56        parent::__construct($level, $bubble);
57    }
58
59    /**
60     * {@inheritDoc}
61     */
62    protected function write(array $record): void
63    {
64        $this->writeZendMonitorCustomEvent(
65            Logger::getLevelName($record['level']),
66            $record['message'],
67            $record['formatted'],
68            $this->levelMap[$record['level']]
69        );
70    }
71
72    /**
73     * Write to Zend Monitor Events
74     * @param string $type      Text displayed in "Class Name (custom)" field
75     * @param string $message   Text displayed in "Error String"
76     * @param array  $formatted Displayed in Custom Variables tab
77     * @param int    $severity  Set the event severity level (-1,0,1)
78     *
79     * @phpstan-param FormattedRecord $formatted
80     */
81    protected function writeZendMonitorCustomEvent(string $type, string $message, array $formatted, int $severity): void
82    {
83        zend_monitor_custom_event($type, $message, $formatted, $severity);
84    }
85
86    /**
87     * {@inheritDoc}
88     */
89    public function getDefaultFormatter(): FormatterInterface
90    {
91        return new NormalizerFormatter();
92    }
93
94    /**
95     * @return array<int, int>
96     */
97    public function getLevelMap(): array
98    {
99        return $this->levelMap;
100    }
101}
102