Zend_Log-Writers.xml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.log.writers" xmlns:xi="http://www.w3.org/2001/XInclude">
  4. <title>Writers</title>
  5. <para>
  6. A Writer is an object that inherits from <classname>Zend_Log_Writer_Abstract</classname>.
  7. A Writer's responsibility is to record log data to a storage backend.
  8. </para>
  9. <sect2 id="zend.log.writers.stream">
  10. <title>Writing to Streams</title>
  11. <para>
  12. <classname>Zend_Log_Writer_Stream</classname> sends log
  13. data to a <ulink url="http://www.php.net/stream">PHP stream</ulink>.
  14. </para>
  15. <para>
  16. To write log data to the <acronym>PHP</acronym> output buffer, use the URL
  17. <filename>php://output</filename>. Alternatively, you can send log data directly to a
  18. stream like <constant>STDERR</constant> (<filename>php://stderr</filename>).
  19. </para>
  20. <programlisting language="php"><![CDATA[
  21. $writer = new Zend_Log_Writer_Stream('php://output');
  22. $logger = new Zend_Log($writer);
  23. $logger->info('Informational message');
  24. ]]></programlisting>
  25. <para>
  26. To write data to a file, use one of the
  27. <ulink url="http://www.php.net/manual/en/wrappers.php#wrappers.file">Filesystem
  28. URLs</ulink>:
  29. </para>
  30. <programlisting language="php"><![CDATA[
  31. $writer = new Zend_Log_Writer_Stream('/path/to/logfile');
  32. $logger = new Zend_Log($writer);
  33. $logger->info('Informational message');
  34. ]]></programlisting>
  35. <para>
  36. By default, the stream opens in the append mode ("a").
  37. To open it with a different mode, the <classname>Zend_Log_Writer_Stream</classname>
  38. constructor accepts an optional second parameter for the mode.
  39. </para>
  40. <para>
  41. The constructor of <classname>Zend_Log_Writer_Stream</classname> also accepts an
  42. existing stream resource:
  43. </para>
  44. <programlisting language="php"><![CDATA[
  45. $stream = @fopen('/path/to/logfile', 'a', false);
  46. if (! $stream) {
  47. throw new Exception('Failed to open stream');
  48. }
  49. $writer = new Zend_Log_Writer_Stream($stream);
  50. $logger = new Zend_Log($writer);
  51. $logger->info('Informational message');
  52. ]]></programlisting>
  53. <para>
  54. You cannot specify the mode for existing stream resources. Doing so
  55. causes a <classname>Zend_Log_Exception</classname> to be thrown.
  56. </para>
  57. </sect2>
  58. <sect2 id="zend.log.writers.database">
  59. <title>Writing to Databases</title>
  60. <para>
  61. <classname>Zend_Log_Writer_Db</classname> writes log information to a database table
  62. using <classname>Zend_Db</classname>. The constructor of
  63. <classname>Zend_Log_Writer_Db</classname> receives a
  64. <classname>Zend_Db_Adapter</classname> instance, a table name, and a mapping of database
  65. columns to event data items:
  66. </para>
  67. <programlisting language="php"><![CDATA[
  68. $params = array ('host' => '127.0.0.1',
  69. 'username' => 'malory',
  70. 'password' => '******',
  71. 'dbname' => 'camelot');
  72. $db = Zend_Db::factory('PDO_MYSQL', $params);
  73. $columnMapping = array('lvl' => 'priority', 'msg' => 'message');
  74. $writer = new Zend_Log_Writer_Db($db, 'log_table_name', $columnMapping);
  75. $logger = new Zend_Log($writer);
  76. $logger->info('Informational message');
  77. ]]></programlisting>
  78. <para>
  79. The example above writes a single row of log data to the database table named
  80. 'log_table_name' table. The database column named 'lvl'
  81. receives the priority number and the column named 'msg' receives the log message.
  82. </para>
  83. </sect2>
  84. <xi:include href="Zend_Log-Writers-Firebug.xml" />
  85. <xi:include href="Zend_Log-Writers-Mail.xml" />
  86. <xi:include href="Zend_Log-Writers-Syslog.xml" />
  87. <xi:include href="Zend_Log-Writers-ZendMonitor.xml" />
  88. <sect2 id="zend.log.writers.null">
  89. <title>Stubbing Out the Writer</title>
  90. <para>
  91. The <classname>Zend_Log_Writer_Null</classname> is a stub that does not write log data
  92. to anything. It is useful for disabling logging or stubbing out logging during tests:
  93. </para>
  94. <programlisting language="php"><![CDATA[
  95. $writer = new Zend_Log_Writer_Null;
  96. $logger = new Zend_Log($writer);
  97. // goes nowhere
  98. $logger->info('Informational message');
  99. ]]></programlisting>
  100. </sect2>
  101. <sect2 id="zend.log.writers.mock">
  102. <title>Testing with the Mock</title>
  103. <para>
  104. The <classname>Zend_Log_Writer_Mock</classname> is a very simple writer that records
  105. the raw data it receives in an array exposed as a public property.
  106. </para>
  107. <programlisting language="php"><![CDATA[
  108. $mock = new Zend_Log_Writer_Mock;
  109. $logger = new Zend_Log($mock);
  110. $logger->info('Informational message');
  111. var_dump($mock->events[0]);
  112. // Array
  113. // (
  114. // [timestamp] => 2007-04-06T07:16:37-07:00
  115. // [message] => Informational message
  116. // [priority] => 6
  117. // [priorityName] => INFO
  118. // )
  119. ]]></programlisting>
  120. <para>
  121. To clear the events logged by the mock, simply set
  122. <command>$mock->events = array()</command>.
  123. </para>
  124. </sect2>
  125. <sect2 id="zend.log.writers.compositing">
  126. <title>Compositing Writers</title>
  127. <para>
  128. There is no composite Writer object. However, a Log instance can write
  129. to any number of Writers. To do this, use the <methodname>addWriter()</methodname>
  130. method:
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. $writer1 = new Zend_Log_Writer_Stream('/path/to/first/logfile');
  134. $writer2 = new Zend_Log_Writer_Stream('/path/to/second/logfile');
  135. $logger = new Zend_Log();
  136. $logger->addWriter($writer1);
  137. $logger->addWriter($writer2);
  138. // goes to both writers
  139. $logger->info('Informational message');
  140. ]]></programlisting>
  141. </sect2>
  142. </sect1>