Zend_Log-Overview.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.log.overview">
  4. <title>Overview</title>
  5. <para>
  6. <classname>Zend_Log</classname> is a component for general purpose logging.
  7. It supports multiple log backends, formatting messages sent to the log,
  8. and filtering messages from being logged. These functions are divided
  9. into the following objects:
  10. <itemizedlist>
  11. <listitem>
  12. <para>
  13. A Log (instance of <classname>Zend_Log</classname>) is the object that your
  14. application uses the most. You can have as many Log objects as you
  15. like; they do not interact. A Log object must contain at
  16. least one Writer, and can optionally contain one or more Filters.
  17. </para>
  18. </listitem>
  19. <listitem>
  20. <para>
  21. A Writer (inherits from <classname>Zend_Log_Writer_Abstract</classname>) is
  22. responsible for saving data to storage.
  23. </para>
  24. </listitem>
  25. <listitem>
  26. <para>
  27. A Filter (implements <classname>Zend_Log_Filter_Interface</classname>)
  28. blocks log data from being saved. A filter may be applied to an
  29. individual Writer, or to a Log where it is applied before all
  30. Writers. In either case, filters may be chained.
  31. </para>
  32. </listitem>
  33. <listitem>
  34. <para>
  35. A Formatter (implements <classname>Zend_Log_Formatter_Interface</classname>)
  36. can format the log data before it is written by a Writer. Each
  37. Writer has exactly one Formatter.
  38. </para>
  39. </listitem>
  40. </itemizedlist>
  41. </para>
  42. <sect2 id="zend.log.overview.creating-a-logger">
  43. <title>Creating a Log</title>
  44. <para>
  45. To get started logging, instantiate a Writer and then pass it to a Log instance:
  46. <programlisting language="php"><![CDATA[
  47. $logger = new Zend_Log();
  48. $writer = new Zend_Log_Writer_Stream('php://output');
  49. $logger->addWriter($writer);
  50. ]]></programlisting>
  51. It is important to note that the Log must
  52. have at least one Writer. You can add any number of Writers using the
  53. Log's <methodname>addWriter()</methodname> method.
  54. </para>
  55. <para>
  56. Alternatively, you can pass a Writer directly to constructor of Log as
  57. a shortcut:
  58. <programlisting language="php"><![CDATA[
  59. $writer = new Zend_Log_Writer_Stream('php://output');
  60. $logger = new Zend_Log($writer);
  61. ]]></programlisting>
  62. The Log is now ready to use.
  63. </para>
  64. </sect2>
  65. <sect2 id="zend.log.overview.logging-messages">
  66. <title>Logging Messages</title>
  67. <para>
  68. To log a message, call the <methodname>log()</methodname> method of a Log instance
  69. and pass it the message with a corresponding priority:
  70. <programlisting language="php"><![CDATA[
  71. $logger->log('Informational message', Zend_Log::INFO);
  72. ]]></programlisting>
  73. The first parameter of the <methodname>log()</methodname> method is a string
  74. <code>message</code> and the second parameter is an integer <code>priority</code>. The
  75. priority must be one of the priorities recognized by the Log instance. This is explained
  76. in the next section.
  77. </para>
  78. <para>
  79. A shortcut is also available. Instead of calling the <methodname>log()</methodname>
  80. method, you can call a method by the same name as the priority:
  81. <programlisting language="php"><![CDATA[
  82. $logger->log('Informational message', Zend_Log::INFO);
  83. $logger->info('Informational message');
  84. $logger->log('Emergency message', Zend_Log::EMERG);
  85. $logger->emerg('Emergency message');
  86. ]]></programlisting>
  87. </para>
  88. </sect2>
  89. <sect2 id="zend.log.overview.destroying-a-logger">
  90. <title>Destroying a Log</title>
  91. <para>
  92. If the Log object is no longer needed, set the variable containing it to
  93. <constant>NULL</constant> to destroy it. This will automatically call the
  94. <methodname>shutdown()</methodname> instance method of each attached Writer before
  95. the Log object is destroyed:
  96. <programlisting language="php"><![CDATA[
  97. $logger = null;
  98. ]]></programlisting>
  99. Explicitly destroying the log in this way is optional and is performed
  100. automatically at <acronym>PHP</acronym> shutdown.
  101. </para>
  102. </sect2>
  103. <sect2 id="zend.log.overview.builtin-priorities">
  104. <title>Using Built-in Priorities</title>
  105. <para>
  106. The <classname>Zend_Log</classname> class defines the following priorities:
  107. <programlisting language="php"><![CDATA[
  108. EMERG = 0; // Emergency: system is unusable
  109. ALERT = 1; // Alert: action must be taken immediately
  110. CRIT = 2; // Critical: critical conditions
  111. ERR = 3; // Error: error conditions
  112. WARN = 4; // Warning: warning conditions
  113. NOTICE = 5; // Notice: normal but significant condition
  114. INFO = 6; // Informational: informational messages
  115. DEBUG = 7; // Debug: debug messages
  116. ]]></programlisting>
  117. These priorities are always available, and a convenience method of the same name
  118. is available for each one.
  119. </para>
  120. <para>
  121. The priorities are not arbitrary. They come from the BSD <code>syslog</code> protocol,
  122. which is described in <ulink url="http://tools.ietf.org/html/rfc3164">RFC-3164</ulink>.
  123. The names and corresponding priority numbers are also
  124. compatible with another <acronym>PHP</acronym> logging system,
  125. <ulink url="http://pear.php.net/package/log">PEAR Log</ulink>,
  126. which perhaps promotes interoperability between it and <classname>Zend_Log</classname>.
  127. </para>
  128. <para>
  129. Priority numbers descend in order of importance. <constant>EMERG</constant> (0)
  130. is the most important priority. <constant>DEBUG</constant> (7) is the least
  131. important priority of the built-in priorities. You may define priorities
  132. of lower importance than <constant>DEBUG</constant>. When
  133. selecting the priority for your log message, be aware of this priority
  134. hierarchy and choose appropriately.
  135. </para>
  136. </sect2>
  137. <sect2 id="zend.log.overview.user-defined-priorities">
  138. <title>Adding User-defined Priorities</title>
  139. <para>
  140. User-defined priorities can be added at runtime using the Log's
  141. <methodname>addPriority()</methodname> method:
  142. <programlisting language="php"><![CDATA[
  143. $logger->addPriority('FOO', 8);
  144. ]]></programlisting>
  145. The snippet above creates a new priority, <constant>FOO</constant>, whose
  146. value is <code>8</code>. The new priority is then available for logging:
  147. <programlisting language="php"><![CDATA[
  148. $logger->log('Foo message', 8);
  149. $logger->foo('Foo Message');
  150. ]]></programlisting>
  151. New priorities cannot overwrite existing ones.
  152. </para>
  153. </sect2>
  154. <sect2 id="zend.log.overview.understanding-fields">
  155. <title>Understanding Log Events</title>
  156. <para>
  157. When you call the <methodname>log()</methodname> method or one of its shortcuts, a
  158. log event is created. This is simply an associative array with data
  159. describing the event that is passed to the writers. The following keys
  160. are always created in this array: <code>timestamp</code>,
  161. <code>message</code>, <code>priority</code>, and
  162. <code>priorityName</code>.
  163. </para>
  164. <para>
  165. The creation of the <code>event</code> array is completely transparent.
  166. However, knowledge of the <code>event</code> array is required for adding an
  167. item that does not exist in the default set above.
  168. </para>
  169. <para>
  170. To add a new item to every future event, call the <methodname>setEventItem()</methodname>
  171. method giving a key and a value:
  172. <programlisting language="php"><![CDATA[
  173. $logger->setEventItem('pid', getmypid());
  174. ]]></programlisting>
  175. The example above sets a new item named <code>pid</code> and populates
  176. it with the PID of the current process. Once a new item has been
  177. set, it is available automatically to all writers along with all of the
  178. other data event data during logging. An item can be overwritten at any
  179. time by calling the <methodname>setEventItem()</methodname> method again.
  180. </para>
  181. <para>
  182. Setting a new event item with <methodname>setEventItem()</methodname> causes the
  183. new item to be sent to all writers of the logger. However, this does
  184. not guarantee that the writers actually record the item. This is
  185. because the writers won't know what to do with it unless a formatter
  186. object is informed of the new item. Please see the section on Formatters
  187. to learn more.
  188. </para>
  189. </sect2>
  190. <sect2 id="zend.log.overview.as-errorHandler">
  191. <title>Log PHP Errors</title>
  192. <para>
  193. Zend_Log can also be used to log <acronym>PHP</acronym> errors. Calling
  194. <methodname>registerErrorHandler()</methodname> will add Zend_Log before the current
  195. error handler, and will pass the error along as well.
  196. </para>
  197. <table id="zend.log.overview.as-errorHandler.properties.table-1">
  198. <title>Zend_Log events from PHP errors have the additional fields matching
  199. <methodname>handler ( int $errno , string $errstr [, string $errfile [, int $errline [, array $errcontext ]]] )</methodname>
  200. from <ulink url="http://us3.php.net/manual/en/function.set-error-handler.php">set_error_handler</ulink></title>
  201. <tgroup cols="3">
  202. <thead>
  203. <row>
  204. <entry>Name</entry>
  205. <entry>Error Handler Paramater</entry>
  206. <entry>Description</entry>
  207. </row>
  208. </thead>
  209. <tbody>
  210. <row>
  211. <entry>message</entry>
  212. <entry>errstr</entry>
  213. <entry>Contains the error message, as a string.</entry>
  214. </row>
  215. <row>
  216. <entry>errno</entry>
  217. <entry>errno</entry>
  218. <entry>Contains the level of the error raised, as an integer.</entry>
  219. </row>
  220. <row>
  221. <entry>file</entry>
  222. <entry>errfile</entry>
  223. <entry>Contains the filename that the error was raised in, as a string.</entry>
  224. </row>
  225. <row>
  226. <entry>line</entry>
  227. <entry>errline</entry>
  228. <entry>Contains the line number the error was raised at, as an integer. </entry>
  229. </row>
  230. <row>
  231. <entry>context</entry>
  232. <entry>errcontext</entry>
  233. <entry>(optional) An array that points to the active symbol table at the point the
  234. error occurred. In other words, errcontext will contain an array of every variable
  235. that existed in the scope the error was triggered in. User error handler must not
  236. modify error context.</entry>
  237. </row>
  238. </tbody>
  239. </tgroup>
  240. </table>
  241. </sect2>
  242. </sect1>