1<?php
2
3/**
4 * @see       https://github.com/laminas/laminas-log for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-log/blob/master/COPYRIGHT.md
6 * @license   https://github.com/laminas/laminas-log/blob/master/LICENSE.md New BSD License
7 */
8
9namespace Laminas\Log\Writer;
10
11class ZendMonitor extends AbstractWriter
12{
13    /**
14     * Is Zend Monitor enabled?
15     *
16     * @var bool
17     */
18    protected $isEnabled = true;
19
20    /**
21     * Is this for a Zend Server instance?
22     *
23     * @var bool
24     */
25    protected $isZendServer = false;
26
27    /**
28     * Constructor
29     *
30     * @param array|\Traversable|null $options
31     * @return ZendMonitor
32     */
33    public function __construct($options = null)
34    {
35        parent::__construct($options);
36
37        if (! function_exists('monitor_custom_event')) {
38            $this->isEnabled = false;
39        }
40        if (function_exists('zend_monitor_custom_event')) {
41            $this->isZendServer = true;
42        }
43    }
44
45    /**
46     * Is logging to this writer enabled?
47     *
48     * If the Zend Monitor extension is not enabled, this log writer will
49     * fail silently. You can query this method to determine if the log
50     * writer is enabled.
51     *
52     * @return bool
53     */
54    public function isEnabled()
55    {
56        return $this->isEnabled;
57    }
58
59    /**
60     * Log a message to this writer.
61     *
62     * @param array $event log data event
63     * @return void
64     */
65    public function write(array $event)
66    {
67        if (! $this->isEnabled()) {
68            return;
69        }
70
71        parent::write($event);
72    }
73
74    /**
75     * Write a message to the log.
76     *
77     * @param array $event log data event
78     * @return void
79     */
80    protected function doWrite(array $event)
81    {
82        $priority = $event['priority'];
83        $message  = $event['message'];
84        unset($event['priority'], $event['message']);
85
86        if (! empty($event)) {
87            if ($this->isZendServer) {
88                // On Zend Server; third argument should be the event
89                zend_monitor_custom_event($priority, $message, $event);
90            } else {
91                // On Zend Platform; third argument is severity -- either
92                // 0 or 1 -- and fourth is optional (event)
93                // Severity is either 0 (normal) or 1 (severe); classifying
94                // notice, info, and debug as "normal", and all others as
95                // "severe"
96                monitor_custom_event($priority, $message, ($priority > 4) ? 0 : 1, $event);
97            }
98        } else {
99            monitor_custom_event($priority, $message);
100        }
101    }
102}
103