diff options
author | vipsoft <vipsoft@59fd770c-687e-43c8-a1e3-f5a4ff64c105> | 2009-09-07 22:40:52 +0400 |
---|---|---|
committer | vipsoft <vipsoft@59fd770c-687e-43c8-a1e3-f5a4ff64c105> | 2009-09-07 22:40:52 +0400 |
commit | 9ce132f774176704fe8a23ce1dc169a9e94ac465 (patch) | |
tree | b205427c2cbb4c03d741b62ed6cd64f763a99bef /libs/Zend/Log/Writer | |
parent | ab982e606b2dc25ef7ea9ba9496416972e82df6b (diff) |
Fixes #497 - update to Zend Framework 1.9.2 (subset); remove svn:keywords to preserve the original $Id; misc changes to handle fetchRow() sometimes returning null (instead of false)
Diffstat (limited to 'libs/Zend/Log/Writer')
-rw-r--r-- | libs/Zend/Log/Writer/Abstract.php | 20 | ||||
-rw-r--r-- | libs/Zend/Log/Writer/Db.php | 22 | ||||
-rw-r--r-- | libs/Zend/Log/Writer/Firebug.php | 188 | ||||
-rw-r--r-- | libs/Zend/Log/Writer/Mail.php | 295 | ||||
-rw-r--r-- | libs/Zend/Log/Writer/Mock.php | 20 | ||||
-rw-r--r-- | libs/Zend/Log/Writer/Null.php | 12 | ||||
-rw-r--r-- | libs/Zend/Log/Writer/Stream.php | 22 | ||||
-rw-r--r-- | libs/Zend/Log/Writer/Syslog.php | 175 |
8 files changed, 708 insertions, 46 deletions
diff --git a/libs/Zend/Log/Writer/Abstract.php b/libs/Zend/Log/Writer/Abstract.php index aa5cee4750..3e3be2d6cd 100644 --- a/libs/Zend/Log/Writer/Abstract.php +++ b/libs/Zend/Log/Writer/Abstract.php @@ -15,25 +15,22 @@ * @category Zend * @package Zend_Log * @subpackage Writer - * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id$ + * @version $Id: Abstract.php 16219 2009-06-21 19:45:39Z thomas $ */ /** Zend_Log_Filter_Priority */ require_once 'Zend/Log/Filter/Priority.php'; -/** Zend_Log_Exception */ -require_once 'Zend/Log/Exception.php'; - /** * @category Zend * @package Zend_Log * @subpackage Writer - * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id$ - */ + * @version $Id: Abstract.php 16219 2009-06-21 19:45:39Z thomas $ + */ abstract class Zend_Log_Writer_Abstract { /** @@ -49,7 +46,7 @@ abstract class Zend_Log_Writer_Abstract /** * Add a filter specific to this writer. - * + * * @param Zend_Log_Filter_Interface $filter * @return void */ @@ -86,7 +83,8 @@ abstract class Zend_Log_Writer_Abstract * @param Zend_Log_Formatter_Interface $formatter * @return void */ - public function setFormatter($formatter) { + public function setFormatter($formatter) + { $this->_formatter = $formatter; } @@ -106,4 +104,4 @@ abstract class Zend_Log_Writer_Abstract */ abstract protected function _write($event); -} +}
\ No newline at end of file diff --git a/libs/Zend/Log/Writer/Db.php b/libs/Zend/Log/Writer/Db.php index dcc81cce9c..2130efe28e 100644 --- a/libs/Zend/Log/Writer/Db.php +++ b/libs/Zend/Log/Writer/Db.php @@ -15,9 +15,9 @@ * @category Zend * @package Zend_Log * @subpackage Writer - * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id$ + * @version $Id: Db.php 16219 2009-06-21 19:45:39Z thomas $ */ /** Zend_Log_Writer_Abstract */ @@ -27,10 +27,10 @@ require_once 'Zend/Log/Writer/Abstract.php'; * @category Zend * @package Zend_Log * @subpackage Writer - * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id$ - */ + * @version $Id: Db.php 16219 2009-06-21 19:45:39Z thomas $ + */ class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract { /** @@ -69,8 +69,9 @@ class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract /** * Formatting is not possible on this writer */ - public function setFormatter($formatter) + public function setFormatter($formatter) { + require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception(get_class() . ' does not support formatting'); } @@ -79,7 +80,7 @@ class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract * * @return void */ - public function shutdown() + public function shutdown() { $this->_db = null; } @@ -93,9 +94,10 @@ class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract protected function _write($event) { if ($this->_db === null) { - throw new Zend_Log_Exception('Database adapter instance has been removed by shutdown'); + require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Database adapter is null'); } - + if ($this->_columnMap === null) { $dataToInsert = $event; } else { @@ -104,7 +106,7 @@ class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract $dataToInsert[$columnName] = $event[$fieldKey]; } } - + $this->_db->insert($this->_table, $dataToInsert); } diff --git a/libs/Zend/Log/Writer/Firebug.php b/libs/Zend/Log/Writer/Firebug.php new file mode 100644 index 0000000000..49eeae9a2d --- /dev/null +++ b/libs/Zend/Log/Writer/Firebug.php @@ -0,0 +1,188 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Log + * @subpackage Writer + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Firebug.php 16971 2009-07-22 18:05:45Z mikaelkael $ + */ + +/** Zend_Log */ +require_once 'Zend/Log.php'; + +/** Zend_Log_Writer_Abstract */ +require_once 'Zend/Log/Writer/Abstract.php'; + +/** Zend_Log_Formatter_Firebug */ +require_once 'Zend/Log/Formatter/Firebug.php'; + +/** Zend_Wildfire_Plugin_FirePhp */ +require_once 'Zend/Wildfire/Plugin/FirePhp.php'; + +/** + * Writes log messages to the Firebug Console via FirePHP. + * + * @category Zend + * @package Zend_Log + * @subpackage Writer + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Log_Writer_Firebug extends Zend_Log_Writer_Abstract +{ + + /** + * Maps logging priorities to logging display styles + * @var array + */ + protected $_priorityStyles = array(Zend_Log::EMERG => Zend_Wildfire_Plugin_FirePhp::ERROR, + Zend_Log::ALERT => Zend_Wildfire_Plugin_FirePhp::ERROR, + Zend_Log::CRIT => Zend_Wildfire_Plugin_FirePhp::ERROR, + Zend_Log::ERR => Zend_Wildfire_Plugin_FirePhp::ERROR, + Zend_Log::WARN => Zend_Wildfire_Plugin_FirePhp::WARN, + Zend_Log::NOTICE => Zend_Wildfire_Plugin_FirePhp::INFO, + Zend_Log::INFO => Zend_Wildfire_Plugin_FirePhp::INFO, + Zend_Log::DEBUG => Zend_Wildfire_Plugin_FirePhp::LOG); + + /** + * The default logging style for un-mapped priorities + * @var string + */ + protected $_defaultPriorityStyle = Zend_Wildfire_Plugin_FirePhp::LOG; + + /** + * Flag indicating whether the log writer is enabled + * @var boolean + */ + protected $_enabled = true; + + /** + * Class constructor + */ + public function __construct() + { + if (php_sapi_name()=='cli') { + $this->setEnabled(false); + } + + $this->_formatter = new Zend_Log_Formatter_Firebug(); + } + + /** + * Enable or disable the log writer. + * + * @param boolean $enabled Set to TRUE to enable the log writer + * @return boolean The previous value. + */ + public function setEnabled($enabled) + { + $previous = $this->_enabled; + $this->_enabled = $enabled; + return $previous; + } + + /** + * Determine if the log writer is enabled. + * + * @return boolean Returns TRUE if the log writer is enabled. + */ + public function getEnabled() + { + return $this->_enabled; + } + + /** + * Set the default display style for user-defined priorities + * + * @param string $style The default log display style + * @return string Returns previous default log display style + */ + public function setDefaultPriorityStyle($style) + { + $previous = $this->_defaultPriorityStyle; + $this->_defaultPriorityStyle = $style; + return $previous; + } + + /** + * Get the default display style for user-defined priorities + * + * @return string Returns the default log display style + */ + public function getDefaultPriorityStyle() + { + return $this->_defaultPriorityStyle; + } + + /** + * Set a display style for a logging priority + * + * @param int $priority The logging priority + * @param string $style The logging display style + * @return string|boolean The previous logging display style if defined or TRUE otherwise + */ + public function setPriorityStyle($priority, $style) + { + $previous = true; + if (array_key_exists($priority,$this->_priorityStyles)) { + $previous = $this->_priorityStyles[$priority]; + } + $this->_priorityStyles[$priority] = $style; + return $previous; + } + + /** + * Get a display style for a logging priority + * + * @param int $priority The logging priority + * @return string|boolean The logging display style if defined or FALSE otherwise + */ + public function getPriorityStyle($priority) + { + if (array_key_exists($priority,$this->_priorityStyles)) { + return $this->_priorityStyles[$priority]; + } + return false; + } + + /** + * Log a message to the Firebug Console. + * + * @param array $event The event data + * @return void + */ + protected function _write($event) + { + if (!$this->getEnabled()) { + return; + } + + if (array_key_exists($event['priority'],$this->_priorityStyles)) { + $type = $this->_priorityStyles[$event['priority']]; + } else { + $type = $this->_defaultPriorityStyle; + } + + $message = $this->_formatter->format($event); + + $label = isset($event['firebugLabel'])?$event['firebugLabel']:null; + + Zend_Wildfire_Plugin_FirePhp::getInstance()->send($message, + $label, + $type, + array('traceOffset'=>6)); + } +} diff --git a/libs/Zend/Log/Writer/Mail.php b/libs/Zend/Log/Writer/Mail.php new file mode 100644 index 0000000000..068bd33dc1 --- /dev/null +++ b/libs/Zend/Log/Writer/Mail.php @@ -0,0 +1,295 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Log + * @subpackage Writer + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Mail.php 16219 2009-06-21 19:45:39Z thomas $ + */ + +/** Zend_Log_Writer_Abstract */ +require_once 'Zend/Log/Writer/Abstract.php'; + +/** Zend_Log_Exception */ +require_once 'Zend/Log/Exception.php'; + +/** Zend_Log_Formatter_Simple*/ +require_once 'Zend/Log/Formatter/Simple.php'; + +/** + * Class used for writing log messages to email via Zend_Mail. + * + * Allows for emailing log messages at and above a certain level via a + * Zend_Mail object. Note that this class only sends the email upon + * completion, so any log entries accumulated are sent in a single email. + * + * @category Zend + * @package Zend_Log + * @subpackage Writer + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Mail.php 16219 2009-06-21 19:45:39Z thomas $ + */ +class Zend_Log_Writer_Mail extends Zend_Log_Writer_Abstract +{ + /** + * Array of formatted events to include in message body. + * + * @var array + */ + protected $_eventsToMail = array(); + + /** + * Array of formatted lines for use in an HTML email body; these events + * are formatted with an optional formatter if the caller is using + * Zend_Layout. + * + * @var array + */ + protected $_layoutEventsToMail = array(); + + /** + * Zend_Mail instance to use + * + * @var Zend_Mail + */ + protected $_mail; + + /** + * Zend_Layout instance to use; optional. + * + * @var Zend_Layout + */ + protected $_layout; + + /** + * Optional formatter for use when rendering with Zend_Layout. + * + * @var Zend_Log_Formatter_Interface + */ + protected $_layoutFormatter; + + /** + * Array keeping track of the number of entries per priority level. + * + * @var array + */ + protected $_numEntriesPerPriority = array(); + + /** + * Subject prepend text. + * + * Can only be used of the Zend_Mail object has not already had its + * subject line set. Using this will cause the subject to have the entry + * counts per-priority level appended to it. + * + * @var string|null + */ + protected $_subjectPrependText; + + /** + * Class constructor. + * + * Constructs the mail writer; requires a Zend_Mail instance, and takes an + * optional Zend_Layout instance. If Zend_Layout is being used, + * $this->_layout->events will be set for use in the layout template. + * + * @param Zend_Mail $mail Mail instance + * @param Zend_Layout $layout Layout instance; optional + * @return void + */ + public function __construct(Zend_Mail $mail, Zend_Layout $layout = null) + { + $this->_mail = $mail; + $this->_layout = $layout; + $this->_formatter = new Zend_Log_Formatter_Simple(); + } + + /** + * Places event line into array of lines to be used as message body. + * + * Handles the formatting of both plaintext entries, as well as those + * rendered with Zend_Layout. + * + * @param array $event Event data + * @return void + */ + protected function _write($event) + { + // Track the number of entries per priority level. + if (!isset($this->_numEntriesPerPriority[$event['priorityName']])) { + $this->_numEntriesPerPriority[$event['priorityName']] = 1; + } else { + $this->_numEntriesPerPriority[$event['priorityName']]++; + } + + $formattedEvent = $this->_formatter->format($event); + + // All plaintext events are to use the standard formatter. + $this->_eventsToMail[] = $formattedEvent; + + // If we have a Zend_Layout instance, use a specific formatter for the + // layout if one exists. Otherwise, just use the event with its + // default format. + if ($this->_layout) { + if ($this->_layoutFormatter) { + $this->_layoutEventsToMail[] = + $this->_layoutFormatter->format($event); + } else { + $this->_layoutEventsToMail[] = $formattedEvent; + } + } + } + + /** + * Gets instance of Zend_Log_Formatter_Instance used for formatting a + * message using Zend_Layout, if applicable. + * + * @return Zend_Log_Formatter_Interface|null The formatter, or null. + */ + public function getLayoutFormatter() + { + return $this->_layoutFormatter; + } + + /** + * Sets a specific formatter for use with Zend_Layout events. + * + * Allows use of a second formatter on lines that will be rendered with + * Zend_Layout. In the event that Zend_Layout is not being used, this + * formatter cannot be set, so an exception will be thrown. + * + * @param Zend_Log_Formatter_Interface $formatter + * @return Zend_Log_Writer_Mail + * @throws Zend_Log_Exception + */ + public function setLayoutFormatter(Zend_Log_Formatter_Interface $formatter) + { + if (!$this->_layout) { + throw new Zend_Log_Exception( + 'cannot set formatter for layout; ' . + 'a Zend_Layout instance is not in use'); + } + + $this->_layoutFormatter = $formatter; + return $this; + } + + /** + * Allows caller to have the mail subject dynamically set to contain the + * entry counts per-priority level. + * + * Sets the text for use in the subject, with entry counts per-priority + * level appended to the end. Since a Zend_Mail subject can only be set + * once, this method cannot be used if the Zend_Mail object already has a + * subject set. + * + * @param string $subject Subject prepend text. + * @return Zend_Log_Writer_Mail + */ + public function setSubjectPrependText($subject) + { + if ($this->_mail->getSubject()) { + throw new Zend_Log_Exception( + 'subject already set on mail; ' . + 'cannot set subject prepend text'); + } + + $this->_subjectPrependText = (string) $subject; + return $this; + } + + /** + * Sends mail to recipient(s) if log entries are present. Note that both + * plaintext and HTML portions of email are handled here. + * + * @return void + */ + public function shutdown() + { + // If there are events to mail, use them as message body. Otherwise, + // there is no mail to be sent. + if (empty($this->_eventsToMail)) { + return; + } + + if ($this->_subjectPrependText !== null) { + // Tack on the summary of entries per-priority to the subject + // line and set it on the Zend_Mail object. + $numEntries = $this->_getFormattedNumEntriesPerPriority(); + $this->_mail->setSubject( + "{$this->_subjectPrependText} ({$numEntries})"); + } + + + // Always provide events to mail as plaintext. + $this->_mail->setBodyText(implode('', $this->_eventsToMail)); + + // If a Zend_Layout instance is being used, set its "events" + // value to the lines formatted for use with the layout. + if ($this->_layout) { + // Set the required "messages" value for the layout. Here we + // are assuming that the layout is for use with HTML. + $this->_layout->events = + implode('', $this->_layoutEventsToMail); + + // If an exception occurs during rendering, convert it to a notice + // so we can avoid an exception thrown without a stack frame. + try { + $this->_mail->setBodyHtml($this->_layout->render()); + } catch (Exception $e) { + trigger_error( + "exception occurred when rendering layout; " . + "unable to set html body for message; " . + "message = {$e->getMessage()}; " . + "code = {$e->getCode()}; " . + "exception class = " . get_class($e), + E_USER_NOTICE); + } + } + + // Finally, send the mail. If an exception occurs, convert it into a + // warning-level message so we can avoid an exception thrown without a + // stack frame. + try { + $this->_mail->send(); + } catch (Exception $e) { + trigger_error( + "unable to send log entries via email; " . + "message = {$e->getMessage()}; " . + "code = {$e->getCode()}; " . + "exception class = " . get_class($e), + E_USER_WARNING); + } + } + + /** + * Gets a string of number of entries per-priority level that occurred, or + * an emptry string if none occurred. + * + * @return string + */ + protected function _getFormattedNumEntriesPerPriority() + { + $strings = array(); + + foreach ($this->_numEntriesPerPriority as $priority => $numEntries) { + $strings[] = "{$priority}={$numEntries}"; + } + + return implode(', ', $strings); + } +} diff --git a/libs/Zend/Log/Writer/Mock.php b/libs/Zend/Log/Writer/Mock.php index 2334d5ddb0..2a593c0d9e 100644 --- a/libs/Zend/Log/Writer/Mock.php +++ b/libs/Zend/Log/Writer/Mock.php @@ -15,9 +15,9 @@ * @category Zend * @package Zend_Log * @subpackage Writer - * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id$ + * @version $Id: Mock.php 16971 2009-07-22 18:05:45Z mikaelkael $ */ /** Zend_Log_Writer_Abstract */ @@ -27,22 +27,22 @@ require_once 'Zend/Log/Writer/Abstract.php'; * @category Zend * @package Zend_Log * @subpackage Writer - * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id$ - */ + * @version $Id: Mock.php 16971 2009-07-22 18:05:45Z mikaelkael $ + */ class Zend_Log_Writer_Mock extends Zend_Log_Writer_Abstract { /** * array of log events */ public $events = array(); - + /** * shutdown called? */ public $shutdown = false; - + /** * Write a message to the log. * @@ -53,14 +53,14 @@ class Zend_Log_Writer_Mock extends Zend_Log_Writer_Abstract { $this->events[] = $event; } - + /** * Record shutdown * * @return void - */ + */ public function shutdown() { $this->shutdown = true; } -} +}
\ No newline at end of file diff --git a/libs/Zend/Log/Writer/Null.php b/libs/Zend/Log/Writer/Null.php index 95e68a7190..de791d3a99 100644 --- a/libs/Zend/Log/Writer/Null.php +++ b/libs/Zend/Log/Writer/Null.php @@ -15,9 +15,9 @@ * @category Zend * @package Zend_Log * @subpackage Writer - * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id$ + * @version $Id: Null.php 16219 2009-06-21 19:45:39Z thomas $ */ /** Zend_Log_Writer_Abstract */ @@ -27,10 +27,10 @@ require_once 'Zend/Log/Writer/Abstract.php'; * @category Zend * @package Zend_Log * @subpackage Writer - * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id$ - */ + * @version $Id: Null.php 16219 2009-06-21 19:45:39Z thomas $ + */ class Zend_Log_Writer_Null extends Zend_Log_Writer_Abstract { /** @@ -43,4 +43,4 @@ class Zend_Log_Writer_Null extends Zend_Log_Writer_Abstract { } -} +}
\ No newline at end of file diff --git a/libs/Zend/Log/Writer/Stream.php b/libs/Zend/Log/Writer/Stream.php index 644abe28ed..99b050ad6b 100644 --- a/libs/Zend/Log/Writer/Stream.php +++ b/libs/Zend/Log/Writer/Stream.php @@ -15,9 +15,9 @@ * @category Zend * @package Zend_Log * @subpackage Writer - * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id$ + * @version $Id: Stream.php 16219 2009-06-21 19:45:39Z thomas $ */ /** Zend_Log_Writer_Abstract */ @@ -30,9 +30,9 @@ require_once 'Zend/Log/Formatter/Simple.php'; * @category Zend * @package Zend_Log * @subpackage Writer - * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id$ + * @version $Id: Stream.php 16219 2009-06-21 19:45:39Z thomas $ */ class Zend_Log_Writer_Stream extends Zend_Log_Writer_Abstract { @@ -52,16 +52,19 @@ class Zend_Log_Writer_Stream extends Zend_Log_Writer_Abstract { if (is_resource($streamOrUrl)) { if (get_resource_type($streamOrUrl) != 'stream') { + require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('Resource is not a stream'); } - + if ($mode != 'a') { + require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('Mode cannot be changed on existing streams'); } - + $this->_stream = $streamOrUrl; } else { if (! $this->_stream = @fopen($streamOrUrl, $mode, false)) { + require_once 'Zend/Log/Exception.php'; $msg = "\"$streamOrUrl\" cannot be opened with mode \"$mode\""; throw new Zend_Log_Exception($msg); } @@ -91,10 +94,11 @@ class Zend_Log_Writer_Stream extends Zend_Log_Writer_Abstract protected function _write($event) { $line = $this->_formatter->format($event); - - if ( fwrite($this->_stream, $line) === false) { + + if (false === @fwrite($this->_stream, $line)) { + require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception("Unable to write to stream"); - } + } } } diff --git a/libs/Zend/Log/Writer/Syslog.php b/libs/Zend/Log/Writer/Syslog.php new file mode 100644 index 0000000000..3409ae56dc --- /dev/null +++ b/libs/Zend/Log/Writer/Syslog.php @@ -0,0 +1,175 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Log + * @subpackage Writer + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id: Syslog.php 16971 2009-07-22 18:05:45Z mikaelkael $ + */ + +/** Zend_Log_Writer_Abstract */ +require_once 'Zend/Log/Writer/Abstract.php'; + +/** + * Writes log messages to syslog + * + * @category Zend + * @package Zend_Log + * @subpackage Writer + * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract +{ + /** + * Maps Zend_Log priorities to PHP's syslog priorities + * @var array + */ + protected $_priorities = array( + Zend_Log::EMERG => LOG_EMERG, + Zend_Log::ALERT => LOG_ALERT, + Zend_Log::CRIT => LOG_CRIT, + Zend_Log::ERR => LOG_ERR, + Zend_Log::WARN => LOG_WARNING, + Zend_Log::NOTICE => LOG_NOTICE, + Zend_Log::INFO => LOG_INFO, + Zend_Log::DEBUG => LOG_DEBUG, + ); + + /** + * The default log priority - for unmapped custom priorities + * @var string + */ + protected $_defaultPriority = LOG_NOTICE; + + /** + * Last application name set by a syslog-writer instance + * @var string + */ + protected static $_lastApplication; + + /** + * Last facility name set by a syslog-writer instance + * @var string + */ + protected static $_lastFacility; + + /** + * Application name used by this syslog-writer instance + * @var string + */ + protected $_application = 'Zend_Log'; + + /** + * Facility used by this syslog-writer instance + * @var string + */ + protected $_facility = LOG_USER; + + /** + * Class constructor + * + * @param array $options Array of options; may include "application" and "facility" keys + * @return void + */ + public function __construct(array $params = array()) + { + if (isset($params['application'])) { + $this->_application = $params['application']; + } + if (isset($params['facility'])) { + $this->_facility = $params['facility']; + } + $this->_initializeSyslog(); + } + + /** + * Initialize syslog / set application name and facility + * + * @param string $application Application name + * @param string $facility Syslog facility + * @return void + */ + protected function _initializeSyslog() + { + self::$_lastApplication = $this->_application; + self::$_lastFacility = $this->_facility; + openlog($this->_application, LOG_PID, $this->_facility); + } + + /** + * Set syslog facility + * + * @param string $facility Syslog facility + * @return void + */ + public function setFacility($facility) + { + if ($this->_facility === $facility) { + return; + } + $this->_facility = $facility; + $this->_initializeSyslog(); + } + + /** + * Set application name + * + * @param string $application Application name + * @return void + */ + public function setApplicationName($application) + { + if ($this->_application === $application) { + return; + } + $this->_application = $application; + $this->_initializeSyslog(); + } + + /** + * Close syslog. + * + * @return void + */ + public function shutdown() + { + closelog(); + } + + /** + * Write a message to syslog. + * + * @param array $event event data + * @return void + */ + protected function _write($event) + { + if (array_key_exists($event['priority'], $this->_priorities)) { + $priority = $this->_priorities[$event['priority']]; + } else { + $priority = $this->_defaultPriority; + } + + if ($this->_application !== self::$_lastApplication + || $this->_facility !== self::$_lastFacility) + { + $this->_initializeSyslog(); + } + + syslog($priority, $event['message']); + } +} |