1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect2 id="zend.log.writers.zendmonitor">
<title>Writing to the Zend Server Monitor</title>
<para>
<classname>Zend_Log_Writer_ZendMonitor</classname> allows you to log events via Zend
Server's Monitor <acronym>API</acronym>. This allows you to aggregate log messages for your
entire application environment in a single location. Internally, it simply uses the
<methodname>monitor_custom_event()</methodname> function from the Zend Monitor
<acronym>API</acronym>.
</para>
<para>
One particularly useful feature of the Monitor <acronym>API</acronym> is that it allows you
to specify arbitrary custom information alongside the log message. For instance, if you wish
to log an exception, you can log not just the exception message, but pass the entire
exception object to the function, and then inspect the object within the Zend Server event
monitor.
</para>
<note>
<title>Zend Monitor must be installed and enabled</title>
<para>
In order to use this log writer, Zend Monitor must be both installed and enabled.
However, it is designed such that if Zend Monitor is not detected, it will simply act as
a <constant>NULL</constant> logger.
</para>
</note>
<para>
Instantiating the <classname>ZendMonitor</classname> log writer is trivial:
</para>
<programlisting language="php"><![CDATA[
$writer = new Zend_Log_Writer_ZendMonitor();
$log = new Zend_Log($writer);
]]></programlisting>
<para>
Then, simply log messages as usual:
</para>
<programlisting language="php"><![CDATA[
$log->info('This is a message');
]]></programlisting>
<para>
If you want to specify additional information to log with the event, pass that information
in a second parameter:
</para>
<programlisting language="php"><![CDATA[
$log->info('Exception occurred', $e);
]]></programlisting>
<para>
The second parameter may be a scalar, object, or array; if you need to pass multiple pieces
of information, the best way to do so is to pass an associative array.
</para>
<programlisting language="php"><![CDATA[
$log->info('Exception occurred', array(
'request' => $request,
'exception' => $e,
));
]]></programlisting>
<para>
Within Zend Server, your event is logged as a "custom event". From the "Monitor" tab, select
the "Events" sub-item, and then filter on "Custom" to see custom events.
</para>
<para>
<inlinegraphic fileref="figures/zend.log.writers.zendmonitor-events.png" format="PNG" />
</para>
<para>
Events in Zend Server's Monitor dashboard
</para>
<para>
In this screenshot, the first two events listed are custom events logged via the
<classname>ZendMonitor</classname> log writer. You may then click on an event to view all
information related to it.
</para>
<para>
<inlinegraphic fileref="figures/zend.log.writers.zendmonitor-event.png" format="PNG" />
</para>
<para>
Event detail in Zend Server's Monitor
</para>
<para>
Clicking on the "Custom" sub tab will detail any extra information you logged by passing the
second argument to the logging method. This information will be logged as the
<varname>info</varname> subkey; you can see that the request object was logged in this
example.
</para>
<note>
<title>Integration with Zend_Application</title>
<para>
By default, the <command>zf.sh</command> and <command>zf.bat</command> commands add
configuration for the <link
linkend="zend.application.available-resources.log"><classname>Zend_Application</classname>
log resource</link>, which includes configuration for the
<classname>ZendMonitor</classname> log writer. Additionally, the
<classname>ErrorController</classname> uses the configured logger to log application
exceptions -- providing you with Zend Monitor event integration by default.
</para>
<para>
As noted previously, if the Monitor <acronym>API</acronym> is not detected in your
<acronym>PHP</acronym> installation, the logger will simply act as a
<constant>NULL</constant> logger.
</para>
</note>
</sect2>
|