3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Log\Writer
;
13 use Zend\Log\Exception
;
14 use Zend\Log\Formatter\Simple
as SimpleFormatter
;
15 use Zend\Mail\Message
as MailMessage
;
16 use Zend\Mail\Transport
;
17 use Zend\Mail\Transport\Exception
as TransportException
;
20 * Class used for writing log messages to email via Zend\Mail.
22 * Allows for emailing log messages at and above a certain level via a
23 * Zend\Mail\Message object. Note that this class only sends the email upon
24 * completion, so any log entries accumulated are sent in a single email.
25 * The email is sent using a Zend\Mail\Transport\TransportInterface object
26 * (Sendmail is default).
28 class Mail
extends AbstractWriter
31 * Array of formatted events to include in message body.
35 protected $eventsToMail = array();
38 * Mail message instance to use
45 * Mail transport instance to use; optional.
47 * @var Transport\TransportInterface
52 * Array keeping track of the number of entries per priority level.
56 protected $numEntriesPerPriority = array();
59 * Subject prepend text.
61 * Can only be used of the Zend\Mail object has not already had its
62 * subject line set. Using this will cause the subject to have the entry
63 * counts per-priority level appended to it.
67 protected $subjectPrependText;
72 * @param MailMessage|array|Traversable $mail
73 * @param Transport\TransportInterface $transport Optional
74 * @throws Exception\InvalidArgumentException
76 public function __construct($mail, Transport\TransportInterface
$transport = null)
78 if ($mail instanceof Traversable
) {
79 $mail = iterator_to_array($mail);
82 if (is_array($mail)) {
83 parent
::__construct($mail);
84 if (isset($mail['subject_prepend_text'])) {
85 $this->setSubjectPrependText($mail['subject_prepend_text']);
87 $transport = isset($mail['transport']) ?
$mail['transport'] : null;
88 $mail = isset($mail['mail']) ?
$mail['mail'] : null;
91 // Ensure we have a valid mail message
92 if (!$mail instanceof MailMessage
) {
93 throw new Exception\
InvalidArgumentException(sprintf(
94 'Mail parameter of type %s is invalid; must be of type Zend\Mail\Message',
95 (is_object($mail) ?
get_class($mail) : gettype($mail))
100 // Ensure we have a valid mail transport
101 if (null === $transport) {
102 $transport = new Transport\
Sendmail();
104 if (!$transport instanceof Transport\TransportInterface
) {
105 throw new Exception\
InvalidArgumentException(sprintf(
106 'Transport parameter of type %s is invalid; must be of type Zend\Mail\Transport\TransportInterface',
107 (is_object($transport) ?
get_class($transport) : gettype($transport))
110 $this->setTransport($transport);
112 if ($this->formatter
=== null) {
113 $this->formatter
= new SimpleFormatter();
118 * Set the transport message
120 * @param Transport\TransportInterface $transport
123 public function setTransport(Transport\TransportInterface
$transport)
125 $this->transport
= $transport;
130 * Places event line into array of lines to be used as message body.
132 * @param array $event Event data
134 protected function doWrite(array $event)
136 // Track the number of entries per priority level.
137 if (!isset($this->numEntriesPerPriority
[$event['priorityName']])) {
138 $this->numEntriesPerPriority
[$event['priorityName']] = 1;
140 $this->numEntriesPerPriority
[$event['priorityName']]++
;
143 // All plaintext events are to use the standard formatter.
144 $this->eventsToMail
[] = $this->formatter
->format($event);
148 * Allows caller to have the mail subject dynamically set to contain the
149 * entry counts per-priority level.
151 * Sets the text for use in the subject, with entry counts per-priority
152 * level appended to the end. Since a Zend\Mail\Message subject can only be set
153 * once, this method cannot be used if the Zend\Mail\Message object already has a
156 * @param string $subject Subject prepend text
159 public function setSubjectPrependText($subject)
161 $this->subjectPrependText
= (string) $subject;
166 * Sends mail to recipient(s) if log entries are present. Note that both
167 * plaintext and HTML portions of email are handled here.
169 public function shutdown()
171 // If there are events to mail, use them as message body. Otherwise,
172 // there is no mail to be sent.
173 if (empty($this->eventsToMail
)) {
177 if ($this->subjectPrependText
!== null) {
178 // Tack on the summary of entries per-priority to the subject
179 // line and set it on the Zend\Mail object.
180 $numEntries = $this->getFormattedNumEntriesPerPriority();
181 $this->mail
->setSubject("{$this->subjectPrependText} ({$numEntries})");
184 // Always provide events to mail as plaintext.
185 $this->mail
->setBody(implode(PHP_EOL
, $this->eventsToMail
));
187 // Finally, send the mail. If an exception occurs, convert it into a
188 // warning-level message so we can avoid an exception thrown without a
191 $this->transport
->send($this->mail
);
192 } catch (TransportException\ExceptionInterface
$e) {
194 "unable to send log entries via email; " .
195 "message = {$e->getMessage()}; " .
196 "code = {$e->getCode()}; " .
197 "exception class = " . get_class($e),
203 * Gets a string of number of entries per-priority level that occurred, or
204 * an empty string if none occurred.
208 protected function getFormattedNumEntriesPerPriority()
212 foreach ($this->numEntriesPerPriority
as $priority => $numEntries) {
213 $strings[] = "{$priority}={$numEntries}";
216 return implode(', ', $strings);