Zend_Log-Formatters.xml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <property>event</property> 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
  19. configured automatically when you specify no formatter. The default configuration is
  20. equivalent to the following:
  21. </para>
  22. <programlisting language="php"><![CDATA[
  23. $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
  24. $formatter = new Zend_Log_Formatter_Simple($format);
  25. ]]></programlisting>
  26. <para>
  27. A formatter is set on an individual Writer object using the Writer's
  28. <methodname>setFormatter()</methodname> method:
  29. </para>
  30. <programlisting language="php"><![CDATA[
  31. $writer = new Zend_Log_Writer_Stream('php://output');
  32. $formatter = new Zend_Log_Formatter_Simple('hello %message%' . PHP_EOL);
  33. $writer->setFormatter($formatter);
  34. $logger = new Zend_Log();
  35. $logger->addWriter($writer);
  36. $logger->info('there');
  37. // outputs "hello there"
  38. ]]></programlisting>
  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. <command>%message%</command>). 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
  52. <acronym>XML</acronym> strings. By default, it automatically logs all items in the event
  53. data array:
  54. </para>
  55. <programlisting language="php"><![CDATA[
  56. $writer = new Zend_Log_Writer_Stream('php://output');
  57. $formatter = new Zend_Log_Formatter_Xml();
  58. $writer->setFormatter($formatter);
  59. $logger = new Zend_Log();
  60. $logger->addWriter($writer);
  61. $logger->info('informational message');
  62. ]]></programlisting>
  63. <para>
  64. The code above outputs the following <acronym>XML</acronym> (space added for clarity):
  65. </para>
  66. <programlisting language="xml"><![CDATA[
  67. <logEntry>
  68. <timestamp>2007-04-06T07:24:37-07:00</timestamp>
  69. <message>informational message</message>
  70. <priority>6</priority>
  71. <priorityName>INFO</priorityName>
  72. </logEntry>
  73. ]]></programlisting>
  74. <para>
  75. It's possible to customize the root element as well as specify a mapping
  76. of <acronym>XML</acronym> elements to the items in the event data array. The constructor
  77. of <classname>Zend_Log_Formatter_Xml</classname> accepts a string with the name of
  78. the root element as the first parameter and an associative array with
  79. the element mapping as the second parameter:
  80. </para>
  81. <programlisting language="php"><![CDATA[
  82. $writer = new Zend_Log_Writer_Stream('php://output');
  83. $formatter = new Zend_Log_Formatter_Xml('log',
  84. array('msg' => 'message',
  85. 'level' => 'priorityName')
  86. );
  87. $writer->setFormatter($formatter);
  88. $logger = new Zend_Log();
  89. $logger->addWriter($writer);
  90. $logger->info('informational message');
  91. ]]></programlisting>
  92. <para>
  93. The code above changes the root element from its default of
  94. <property>logEntry</property> to <property>log</property>. It also maps the element
  95. <property>msg</property> to the event data item <property>message</property>. This
  96. results in the following output:
  97. </para>
  98. <programlisting language="xml"><![CDATA[
  99. <log>
  100. <msg>informational message</msg>
  101. <level>INFO</level>
  102. </log>
  103. ]]></programlisting>
  104. </sect2>
  105. </sect1>