Zend_Log-Writers.xml 5.3 KB

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