2
0

Zend_Log-Overview.xml 12 KB

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