2
0

Zend_Log-Writers-Mail.xml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.log.writers.mail">
  4. <title>Writing to Email</title>
  5. <para>
  6. <classname>Zend_Log_Writer_Mail</classname> writes log entries in an email message
  7. by using <classname>Zend_Mail</classname>. The <classname>Zend_Log_Writer_Mail</classname>
  8. constructor takes a <classname>Zend_Mail</classname> object, and an optional
  9. <classname>Zend_Layout</classname> object.
  10. </para>
  11. <para>
  12. The primary use case for <classname>Zend_Log_Writer_Mail</classname> is notifying
  13. developers, systems administrators, or any concerned parties of errors
  14. that might be occurring with <acronym>PHP</acronym>-based scripts.
  15. <classname>Zend_Log_Writer_Mail</classname> was born out of the idea that if
  16. something is broken, a human being needs to be alerted of it immediately
  17. so they can take corrective action.
  18. </para>
  19. <para>
  20. Basic usage is outlined below:
  21. </para>
  22. <programlisting language="php"><![CDATA[
  23. $mail = new Zend_Mail();
  24. $mail->setFrom('errors@example.org')
  25. ->addTo('project_developers@example.org');
  26. $writer = new Zend_Log_Writer_Mail($mail);
  27. // Set subject text for use; summary of number of errors is appended to the
  28. // subject line before sending the message.
  29. $writer->setSubjectPrependText('Errors with script foo.php');
  30. // Only email warning level entries and higher.
  31. $writer->addFilter(Zend_Log::WARN);
  32. $log = new Zend_Log();
  33. $log->addWriter($writer);
  34. // Something bad happened!
  35. $log->error('unable to connect to database');
  36. // On writer shutdown, Zend_Mail::send() is triggered to send an email with
  37. // all log entries at or above the Zend_Log filter level.
  38. ]]></programlisting>
  39. <para>
  40. <classname>Zend_Log_Writer_Mail</classname> will render the email body as plain
  41. text by default.
  42. </para>
  43. <para>
  44. One email is sent containing all log entries at or above the filter
  45. level. For example, if warning-level entries an up are to be emailed,
  46. and two warnings and five errors occur, the resulting email will contain
  47. a total of seven log entries.
  48. </para>
  49. <sect3 id="zend.log.writers.mail.layoutusage">
  50. <title>Zend_Layout Usage</title>
  51. <para>
  52. A <classname>Zend_Layout</classname> instance may be used to generate the
  53. <acronym>HTML</acronym> portion of a multipart email. If a
  54. <classname>Zend_Layout</classname> instance is in use,
  55. <classname>Zend_Log_Writer_Mail</classname> assumes that it is being used to render
  56. <acronym>HTML</acronym> and sets the body <acronym>HTML</acronym> for the message as
  57. the <classname>Zend_Layout</classname>-rendered value.
  58. </para>
  59. <para>
  60. When using <classname>Zend_Log_Writer_Mail</classname> with a
  61. <classname>Zend_Layout</classname> instance, you have the option to set a
  62. custom formatter by using the <methodname>setLayoutFormatter()</methodname>
  63. method. If no <classname>Zend_Layout</classname>-specific entry formatter was
  64. specified, the formatter currently in use will be used. Full usage
  65. of <classname>Zend_Layout</classname> with a custom formatter is outlined
  66. below.
  67. </para>
  68. <programlisting language="php"><![CDATA[
  69. $mail = new Zend_Mail();
  70. $mail->setFrom('errors@example.org')
  71. ->addTo('project_developers@example.org');
  72. // Note that a subject line is not being set on the Zend_Mail instance!
  73. // Use a simple Zend_Layout instance with its defaults.
  74. $layout = new Zend_Layout();
  75. // Create a formatter that wraps the entry in a listitem tag.
  76. $layoutFormatter = new Zend_Log_Formatter_Simple(
  77. '<li>' . Zend_Log_Formatter_Simple::DEFAULT_FORMAT . '</li>'
  78. );
  79. $writer = new Zend_Log_Writer_Mail($mail, $layout);
  80. // Apply the formatter for entries as rendered with Zend_Layout.
  81. $writer->setLayoutFormatter($layoutFormatter);
  82. $writer->setSubjectPrependText('Errors with script foo.php');
  83. $writer->addFilter(Zend_Log::WARN);
  84. $log = new Zend_Log();
  85. $log->addWriter($writer);
  86. // Something bad happened!
  87. $log->error('unable to connect to database');
  88. // On writer shutdown, Zend_Mail::send() is triggered to send an email with
  89. // all log entries at or above the Zend_Log filter level. The email will
  90. // contain both plain text and HTML parts.
  91. ]]></programlisting>
  92. </sect3>
  93. <sect3 id="zend.log.writers.mail.dynamicsubjectline">
  94. <title>Subject Line Error Level Summary</title>
  95. <para>
  96. The <methodname>setSubjectPrependText()</methodname> method may be used in place
  97. of <methodname>Zend_Mail::setSubject()</methodname> to have the email subject
  98. line dynamically written before the email is sent. For example, if
  99. the subject prepend text reads "Errors from script", the subject of
  100. an email generated by <classname>Zend_Log_Writer_Mail</classname> with two
  101. warnings and five errors would be "Errors from script (warn = 2;
  102. error = 5)". If subject prepend text is not in use via
  103. <classname>Zend_Log_Writer_Mail</classname>, the <classname>Zend_Mail</classname>
  104. subject line, if any, is used.
  105. </para>
  106. </sect3>
  107. <sect3 id="zend.log.writers.mail.caveats">
  108. <title>Caveats</title>
  109. <para>
  110. Sending log entries via email can be dangerous. If error conditions
  111. are being improperly handled by your script, or if you're misusing
  112. the error levels, you might find yourself in a situation where you
  113. are sending hundreds or thousands of emails to the recipients
  114. depending on the frequency of your errors.
  115. </para>
  116. <para>
  117. At this time, <classname>Zend_Log_Writer_Mail</classname> does not provide any
  118. mechanism for throttling or otherwise batching up the messages.
  119. Such functionality should be implemented by the consumer if
  120. necessary.
  121. </para>
  122. <para>
  123. Again, <classname>Zend_Log_Writer_Mail</classname>'s primary goal is to
  124. proactively notify a human being of error conditions. If those
  125. errors are being handled in a timely fashion, and safeguards are
  126. being put in place to prevent those circumstances in the future,
  127. then email-based notification of errors can be a valuable tool.
  128. </para>
  129. </sect3>
  130. </sect2>
  131. <!--
  132. vim:se ts=4 sw=4 et:
  133. -->