Zend_Log-Formatters.xml 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.log.formatters">
  4. <title>Formatters</title>
  5. <para>
  6. A Formatter is an object that is responsible for taking an <code>event</code> array
  7. describing a log event and outputting a string with a formatted log line.
  8. </para>
  9. <para>
  10. Some Writers are not line-oriented and cannot use a Formatter. An example is the
  11. Database Writer, which inserts the event items directly into database columns. For
  12. Writers that cannot support a Formatter, an exception is thrown if you attempt to
  13. set a Formatter.
  14. </para>
  15. <sect2 id="zend.log.formatters.simple">
  16. <title>Simple Formatting</title>
  17. <para>
  18. <classname>Zend_Log_Formatter_Simple</classname> is the default formatter. It is configured
  19. automatically when you specify no formatter. The default configuration is equivalent
  20. to the following:
  21. <programlisting language="php"><![CDATA[
  22. $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
  23. $formatter = new Zend_Log_Formatter_Simple($format);
  24. ]]></programlisting>
  25. </para>
  26. <para>
  27. A formatter is set on an individual Writer object using the Writer's
  28. <methodname>setFormatter()</methodname> method:
  29. <programlisting language="php"><![CDATA[
  30. $writer = new Zend_Log_Writer_Stream('php://output');
  31. $formatter = new Zend_Log_Formatter_Simple('hello %message%' . PHP_EOL);
  32. $writer->setFormatter($formatter);
  33. $logger = new Zend_Log();
  34. $logger->addWriter($writer);
  35. $logger->info('there');
  36. // outputs "hello there"
  37. ]]></programlisting>
  38. </para>
  39. <para>
  40. The constructor of <classname>Zend_Log_Formatter_Simple</classname> accepts a single
  41. parameter: the format string. This string contains keys surrounded by
  42. percent signs (e.g. <code>%message%</code>). The format string may
  43. contain any key from the event data array.
  44. You can retrieve the default keys by using the DEFAULT_FORMAT constant from
  45. <classname>Zend_Log_Formatter_Simple</classname>.
  46. </para>
  47. </sect2>
  48. <sect2 id="zend.log.formatters.xml">
  49. <title>Formatting to XML</title>
  50. <para>
  51. <classname>Zend_Log_Formatter_Xml</classname> formats log data into XML strings.
  52. By default, it automatically logs all items in the event data array:
  53. <programlisting language="php"><![CDATA[
  54. $writer = new Zend_Log_Writer_Stream('php://output');
  55. $formatter = new Zend_Log_Formatter_Xml();
  56. $writer->setFormatter($formatter);
  57. $logger = new Zend_Log();
  58. $logger->addWriter($writer);
  59. $logger->info('informational message');
  60. ]]></programlisting>
  61. </para>
  62. <para>
  63. The code above outputs the following XML (space added for clarity):
  64. <programlisting language="xml"><![CDATA[
  65. <logEntry>
  66. <timestamp>2007-04-06T07:24:37-07:00</timestamp>
  67. <message>informational message</message>
  68. <priority>6</priority>
  69. <priorityName>INFO</priorityName>
  70. </logEntry>
  71. ]]></programlisting>
  72. </para>
  73. <para>
  74. It's possible to customize the root element as well as specify a mapping
  75. of XML elements to the items in the event data array. The constructor
  76. of <classname>Zend_Log_Formatter_Xml</classname> accepts a string with the name of
  77. the root element as the first parameter and an associative array with
  78. the element mapping as the second parameter:
  79. <programlisting language="php"><![CDATA[
  80. $writer = new Zend_Log_Writer_Stream('php://output');
  81. $formatter = new Zend_Log_Formatter_Xml('log',
  82. array('msg' => 'message',
  83. 'level' => 'priorityName')
  84. );
  85. $writer->setFormatter($formatter);
  86. $logger = new Zend_Log();
  87. $logger->addWriter($writer);
  88. $logger->info('informational message');
  89. ]]></programlisting>
  90. The code above changes the root element from its default of
  91. <code>logEntry</code> to <code>log</code>. It also maps the element
  92. <code>msg</code> to the event data item <code>message</code>. This
  93. results in the following output:
  94. <programlisting language="xml"><![CDATA[
  95. <log>
  96. <msg>informational message</msg>
  97. <level>INFO</level>
  98. </log>
  99. ]]></programlisting>
  100. </para>
  101. </sect2>
  102. </sect1>