Zend_Log-Writers.xml 5.9 KB

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