2
0

Zend_Log-Overview.xml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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><listitem>
  19. <para>
  20. A Writer (inherits from <classname>Zend_Log_Writer_Abstract</classname>) is
  21. responsible for saving data to storage.
  22. </para>
  23. </listitem><listitem>
  24. <para>
  25. A Filter (implements <classname>Zend_Log_Filter_Interface</classname>)
  26. blocks log data from being saved. A filter may be applied to an
  27. individual Writer, or to a Log where it is applied before all
  28. Writers. In either case, filters may be chained.
  29. </para>
  30. </listitem><listitem>
  31. <para>
  32. A Formatter (implements <classname>Zend_Log_Formatter_Interface</classname>)
  33. can format the log data before it is written by a Writer. Each
  34. Writer has exactly one Formatter.
  35. </para>
  36. </listitem>
  37. </itemizedlist>
  38. </para>
  39. <sect2 id="zend.log.overview.creating-a-logger">
  40. <title>Creating a Log</title>
  41. <para>
  42. To get started logging, instantiate a Writer and then pass it to a Log
  43. instance:
  44. <programlisting language="php"><![CDATA[
  45. $logger = new Zend_Log();
  46. $writer = new Zend_Log_Writer_Stream('php://output');
  47. $logger->addWriter($writer);
  48. ]]></programlisting>
  49. It is important to note that the Log must
  50. have at least one Writer. You can add any number of Writers using the
  51. Log's <code>addWriter()</code> method.
  52. </para>
  53. <para>
  54. Alternatively, you can pass a Writer directly to constructor of Log as
  55. a shortcut:
  56. <programlisting language="php"><![CDATA[
  57. $writer = new Zend_Log_Writer_Stream('php://output');
  58. $logger = new Zend_Log($writer);
  59. ]]></programlisting>
  60. The Log is now ready to use.
  61. </para>
  62. </sect2>
  63. <sect2 id="zend.log.overview.logging-messages">
  64. <title>Logging Messages</title>
  65. <para>
  66. To log a message, call the <code>log()</code> method of a Log instance
  67. and pass it the message with a corresponding priority:
  68. <programlisting language="php"><![CDATA[
  69. $logger->log('Informational message', Zend_Log::INFO);
  70. ]]></programlisting>
  71. The first parameter of the <code>log()</code> method is a string <code>message</code> and the second
  72. parameter is an integer <code>priority</code>. The priority must be one of the priorities recognized
  73. by the Log instance. This is explained in the next section.
  74. </para>
  75. <para>
  76. A shortcut is also available. Instead of calling the <code>log()</code> method, you can
  77. call a method by the same name as the priority:
  78. <programlisting language="php"><![CDATA[
  79. $logger->log('Informational message', Zend_Log::INFO);
  80. $logger->info('Informational message');
  81. $logger->log('Emergency message', Zend_Log::EMERG);
  82. $logger->emerg('Emergency message');
  83. ]]></programlisting>
  84. </para>
  85. </sect2>
  86. <sect2 id="zend.log.overview.destroying-a-logger">
  87. <title>Destroying a Log</title>
  88. <para>
  89. If the Log object is no longer needed, set the variable containing it to
  90. <constant>NULL</constant> to destroy it. This will automatically call the
  91. <code>shutdown()</code> instance method of each attached Writer before
  92. the Log object is destroyed:
  93. <programlisting language="php"><![CDATA[
  94. $logger = null;
  95. ]]></programlisting>
  96. Explicitly destroying the log in this way is optional and is performed
  97. automatically at PHP shutdown.
  98. </para>
  99. </sect2>
  100. <sect2 id="zend.log.overview.builtin-priorities">
  101. <title>Using Built-in Priorities</title>
  102. <para>
  103. The <classname>Zend_Log</classname> class defines the following priorities:
  104. <programlisting language="php"><![CDATA[
  105. EMERG = 0; // Emergency: system is unusable
  106. ALERT = 1; // Alert: action must be taken immediately
  107. CRIT = 2; // Critical: critical conditions
  108. ERR = 3; // Error: error conditions
  109. WARN = 4; // Warning: warning conditions
  110. NOTICE = 5; // Notice: normal but significant condition
  111. INFO = 6; // Informational: informational messages
  112. DEBUG = 7; // Debug: debug messages
  113. ]]></programlisting>
  114. These priorities are always available, and a convenience method of the same name
  115. is available for each one.
  116. </para>
  117. <para>
  118. The priorities are not arbitrary. They come from the BSD <code>syslog</code> protocol,
  119. which is described in <ulink url="http://tools.ietf.org/html/rfc3164">RFC-3164</ulink>.
  120. The names and corresponding priority numbers are also
  121. compatible with another PHP logging system,
  122. <ulink url="http://pear.php.net/package/log">PEAR Log</ulink>,
  123. which perhaps promotes interoperability between it and <classname>Zend_Log</classname>.
  124. </para>
  125. <para>
  126. Priority numbers descend in order of importance. <code>EMERG</code> (0)
  127. is the most important priority. <code>DEBUG</code> (7) is the least
  128. important priority of the built-in priorities. You may define priorities
  129. of lower importance than <code>DEBUG</code>. When
  130. selecting the priority for your log message, be aware of this priority
  131. hierarchy and choose appropriately.
  132. </para>
  133. </sect2>
  134. <sect2 id="zend.log.overview.user-defined-priorities">
  135. <title>Adding User-defined Priorities</title>
  136. <para>
  137. User-defined priorities can be added at runtime using the Log's
  138. <code>addPriority()</code> method:
  139. <programlisting language="php"><![CDATA[
  140. $logger->addPriority('FOO', 8);
  141. ]]></programlisting>
  142. The snippet above creates a new priority, <code>FOO</code>, whose
  143. value is <code>8</code>. The new priority is then available for logging:
  144. <programlisting language="php"><![CDATA[
  145. $logger->log('Foo message', 8);
  146. $logger->foo('Foo Message');
  147. ]]></programlisting>
  148. New priorities cannot overwrite existing ones.
  149. </para>
  150. </sect2>
  151. <sect2 id="zend.log.overview.understanding-fields">
  152. <title>Understanding Log Events</title>
  153. <para>
  154. When you call the <code>log()</code> method or one of its shortcuts, a
  155. log event is created. This is simply an associative array with data
  156. describing the event that is passed to the writers. The following keys
  157. are always created in this array: <code>timestamp</code>,
  158. <code>message</code>, <code>priority</code>, and
  159. <code>priorityName</code>.
  160. </para>
  161. <para>
  162. The creation of the <code>event</code> array is completely transparent.
  163. However, knowledge of the <code>event</code> array is required for adding an
  164. item that does not exist in the default set above.
  165. </para>
  166. <para>
  167. To add a new item to every future event, call the <code>setEventItem()</code>
  168. method giving a key and a value:
  169. <programlisting language="php"><![CDATA[
  170. $logger->setEventItem('pid', getmypid());
  171. ]]></programlisting>
  172. The example above sets a new item named <code>pid</code> and populates
  173. it with the PID of the current process. Once a new item has been
  174. set, it is available automatically to all writers along with all of the
  175. other data event data during logging. An item can be overwritten at any
  176. time by calling the <code>setEventItem()</code> method again.
  177. </para>
  178. <para>
  179. Setting a new event item with <code>setEventItem()</code> causes the
  180. new item to be sent to all writers of the logger. However, this does
  181. not guarantee that the writers actually record the item. This is
  182. because the writers won't know what to do with it unless a formatter
  183. object is informed of the new item. Please see the section on Formatters
  184. to learn more.
  185. </para>
  186. </sect2>
  187. </sect1>