From 20be1c03ec9175c6b7eae1b07fd39a2e7ecc42a1 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 26 Nov 2009 02:41:58 +0000 Subject: [PATCH] MINOR Added Zend_Log thirdparty dependency (merge from r84322) (merged from r92549) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@93532 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- thirdparty/Zend/Log.php | 222 ++++++++++++++++ thirdparty/Zend/Log/.piston.yml | 8 + thirdparty/Zend/Log/Exception.php | 33 +++ thirdparty/Zend/Log/Filter/Interface.php | 41 +++ thirdparty/Zend/Log/Filter/Message.php | 67 +++++ thirdparty/Zend/Log/Filter/Priority.php | 76 ++++++ thirdparty/Zend/Log/Filter/Suppress.php | 66 +++++ thirdparty/Zend/Log/Formatter/Firebug.php | 49 ++++ thirdparty/Zend/Log/Formatter/Interface.php | 41 +++ thirdparty/Zend/Log/Formatter/Simple.php | 85 ++++++ thirdparty/Zend/Log/Formatter/Xml.php | 90 +++++++ thirdparty/Zend/Log/Writer/Abstract.php | 107 ++++++++ thirdparty/Zend/Log/Writer/Db.php | 113 ++++++++ thirdparty/Zend/Log/Writer/Firebug.php | 187 +++++++++++++ thirdparty/Zend/Log/Writer/Mail.php | 278 ++++++++++++++++++++ thirdparty/Zend/Log/Writer/Mock.php | 66 +++++ thirdparty/Zend/Log/Writer/Null.php | 46 ++++ thirdparty/Zend/Log/Writer/Stream.php | 104 ++++++++ 18 files changed, 1679 insertions(+) create mode 100644 thirdparty/Zend/Log.php create mode 100644 thirdparty/Zend/Log/.piston.yml create mode 100644 thirdparty/Zend/Log/Exception.php create mode 100644 thirdparty/Zend/Log/Filter/Interface.php create mode 100644 thirdparty/Zend/Log/Filter/Message.php create mode 100644 thirdparty/Zend/Log/Filter/Priority.php create mode 100644 thirdparty/Zend/Log/Filter/Suppress.php create mode 100644 thirdparty/Zend/Log/Formatter/Firebug.php create mode 100644 thirdparty/Zend/Log/Formatter/Interface.php create mode 100644 thirdparty/Zend/Log/Formatter/Simple.php create mode 100644 thirdparty/Zend/Log/Formatter/Xml.php create mode 100644 thirdparty/Zend/Log/Writer/Abstract.php create mode 100644 thirdparty/Zend/Log/Writer/Db.php create mode 100644 thirdparty/Zend/Log/Writer/Firebug.php create mode 100644 thirdparty/Zend/Log/Writer/Mail.php create mode 100644 thirdparty/Zend/Log/Writer/Mock.php create mode 100644 thirdparty/Zend/Log/Writer/Null.php create mode 100644 thirdparty/Zend/Log/Writer/Stream.php diff --git a/thirdparty/Zend/Log.php b/thirdparty/Zend/Log.php new file mode 100644 index 000000000..d8585fbd1 --- /dev/null +++ b/thirdparty/Zend/Log.php @@ -0,0 +1,222 @@ +_priorities = array_flip($r->getConstants()); + + if ($writer !== null) { + $this->addWriter($writer); + } + } + + /** + * Class destructor. Shutdown log writers + * + * @return void + */ + public function __destruct() + { + foreach($this->_writers as $writer) { + $writer->shutdown(); + } + } + + /** + * Undefined method handler allows a shortcut: + * $log->priorityName('message') + * instead of + * $log->log('message', Zend_Log::PRIORITY_NAME) + * + * @param string $method priority name + * @param string $params message to log + * @return void + * @throws Zend_Log_Exception + */ + public function __call($method, $params) + { + $priority = strtoupper($method); + if (($priority = array_search($priority, $this->_priorities)) !== false) { + $this->log(array_shift($params), $priority); + } else { + /** @see Zend_Log_Exception */ + require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Bad log priority'); + } + } + + /** + * Log a message at a priority + * + * @param string $message Message to log + * @param integer $priority Priority of message + * @return void + * @throws Zend_Log_Exception + */ + public function log($message, $priority) + { + // sanity checks + if (empty($this->_writers)) { + /** @see Zend_Log_Exception */ + require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('No writers were added'); + } + + if (! isset($this->_priorities[$priority])) { + /** @see Zend_Log_Exception */ + require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Bad log priority'); + } + + // pack into event required by filters and writers + $event = array_merge(array('timestamp' => date('c'), + 'message' => $message, + 'priority' => $priority, + 'priorityName' => $this->_priorities[$priority]), + $this->_extras); + + // abort if rejected by the global filters + foreach ($this->_filters as $filter) { + if (! $filter->accept($event)) { + return; + } + } + + // send to each writer + foreach ($this->_writers as $writer) { + $writer->write($event); + } + } + + /** + * Add a custom priority + * + * @param string $name Name of priority + * @param integer $priority Numeric priority + * @throws Zend_Log_InvalidArgumentException + */ + public function addPriority($name, $priority) + { + // Priority names must be uppercase for predictability. + $name = strtoupper($name); + + if (isset($this->_priorities[$priority]) + || array_search($name, $this->_priorities)) { + /** @see Zend_Log_Exception */ + require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Existing priorities cannot be overwritten'); + } + + $this->_priorities[$priority] = $name; + } + + /** + * Add a filter that will be applied before all log writers. + * Before a message will be received by any of the writers, it + * must be accepted by all filters added with this method. + * + * @param int|Zend_Log_Filter_Interface $filter + * @return void + */ + public function addFilter($filter) + { + if (is_integer($filter)) { + /** @see Zend_Log_Filter_Priority */ + require_once 'Zend/Log/Filter/Priority.php'; + $filter = new Zend_Log_Filter_Priority($filter); + } elseif(!is_object($filter) || ! $filter instanceof Zend_Log_Filter_Interface) { + /** @see Zend_Log_Exception */ + require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Invalid filter provided'); + } + + $this->_filters[] = $filter; + } + + /** + * Add a writer. A writer is responsible for taking a log + * message and writing it out to storage. + * + * @param Zend_Log_Writer_Abstract $writer + * @return void + */ + public function addWriter(Zend_Log_Writer_Abstract $writer) + { + $this->_writers[] = $writer; + } + + /** + * Set an extra item to pass to the log writers. + * + * @param $name Name of the field + * @param $value Value of the field + * @return void + */ + public function setEventItem($name, $value) { + $this->_extras = array_merge($this->_extras, array($name => $value)); + } + +} diff --git a/thirdparty/Zend/Log/.piston.yml b/thirdparty/Zend/Log/.piston.yml new file mode 100644 index 000000000..ac72822d0 --- /dev/null +++ b/thirdparty/Zend/Log/.piston.yml @@ -0,0 +1,8 @@ +--- +format: 1 +handler: + piston:remote-revision: 17966 + piston:uuid: 44c647ce-9c0f-0410-b52a-842ac1e357ba +lock: false +repository_url: http://framework.zend.com/svn/framework/standard/tags/release-1.8.1/library/Zend/Log +repository_class: Piston::Svn::Repository diff --git a/thirdparty/Zend/Log/Exception.php b/thirdparty/Zend/Log/Exception.php new file mode 100644 index 000000000..2acd40334 --- /dev/null +++ b/thirdparty/Zend/Log/Exception.php @@ -0,0 +1,33 @@ +_regexp = $regexp; + } + + /** + * Returns TRUE to accept the message, FALSE to block it. + * + * @param array $event event data + * @return boolean accepted? + */ + public function accept($event) + { + return preg_match($this->_regexp, $event['message']) > 0; + } + +} diff --git a/thirdparty/Zend/Log/Filter/Priority.php b/thirdparty/Zend/Log/Filter/Priority.php new file mode 100644 index 000000000..e1783d72a --- /dev/null +++ b/thirdparty/Zend/Log/Filter/Priority.php @@ -0,0 +1,76 @@ +_priority = $priority; + $this->_operator = $operator; + } + + /** + * Returns TRUE to accept the message, FALSE to block it. + * + * @param array $event event data + * @return boolean accepted? + */ + public function accept($event) + { + return version_compare($event['priority'], $this->_priority, $this->_operator); + } + +} diff --git a/thirdparty/Zend/Log/Filter/Suppress.php b/thirdparty/Zend/Log/Filter/Suppress.php new file mode 100644 index 000000000..a8f677776 --- /dev/null +++ b/thirdparty/Zend/Log/Filter/Suppress.php @@ -0,0 +1,66 @@ +_accept = (! $suppress); + } + + /** + * Returns TRUE to accept the message, FALSE to block it. + * + * @param array $event event data + * @return boolean accepted? + */ + public function accept($event) + { + return $this->_accept; + } + +} diff --git a/thirdparty/Zend/Log/Formatter/Firebug.php b/thirdparty/Zend/Log/Formatter/Firebug.php new file mode 100644 index 000000000..3c95c7350 --- /dev/null +++ b/thirdparty/Zend/Log/Formatter/Firebug.php @@ -0,0 +1,49 @@ +_format = $format; + } + + /** + * Formats data into a single line to be written by the writer. + * + * @param array $event event data + * @return string formatted line to write to the log + */ + public function format($event) + { + $output = $this->_format; + foreach ($event as $name => $value) { + + if ((is_object($value) && !method_exists($value,'__toString')) + || is_array($value)) { + + $value = gettype($value); + } + + $output = str_replace("%$name%", $value, $output); + } + return $output; + } + +} diff --git a/thirdparty/Zend/Log/Formatter/Xml.php b/thirdparty/Zend/Log/Formatter/Xml.php new file mode 100644 index 000000000..bbbd3d20a --- /dev/null +++ b/thirdparty/Zend/Log/Formatter/Xml.php @@ -0,0 +1,90 @@ +_rootElement = $rootElement; + $this->_elementMap = $elementMap; + } + + /** + * Formats data into a single line to be written by the writer. + * + * @param array $event event data + * @return string formatted line to write to the log + */ + public function format($event) + { + if ($this->_elementMap === null) { + $dataToInsert = $event; + } else { + $dataToInsert = array(); + foreach ($this->_elementMap as $elementName => $fieldKey) { + $dataToInsert[$elementName] = $event[$fieldKey]; + } + } + + $dom = new DOMDocument(); + $elt = $dom->appendChild(new DOMElement($this->_rootElement)); + + foreach ($dataToInsert as $key => $value) { + if($key == "message") { + $value = htmlspecialchars($value); + } + $elt->appendChild(new DOMElement($key, $value)); + } + + $xml = $dom->saveXML(); + $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml); + + return $xml . PHP_EOL; + } + +} diff --git a/thirdparty/Zend/Log/Writer/Abstract.php b/thirdparty/Zend/Log/Writer/Abstract.php new file mode 100644 index 000000000..307dcff0f --- /dev/null +++ b/thirdparty/Zend/Log/Writer/Abstract.php @@ -0,0 +1,107 @@ +_filters[] = $filter; + } + + /** + * Log a message to this writer. + * + * @param array $event log data event + * @return void + */ + public function write($event) + { + foreach ($this->_filters as $filter) { + if (! $filter->accept($event)) { + return; + } + } + + // exception occurs on error + $this->_write($event); + } + + /** + * Set a new formatter for this writer + * + * @param Zend_Log_Formatter_Interface $formatter + * @return void + */ + public function setFormatter($formatter) + { + $this->_formatter = $formatter; + } + + /** + * Perform shutdown activites such as closing open resources + * + * @return void + */ + public function shutdown() + {} + + /** + * Write a message to the log. + * + * @param array $event log data event + * @return void + */ + abstract protected function _write($event); + +} \ No newline at end of file diff --git a/thirdparty/Zend/Log/Writer/Db.php b/thirdparty/Zend/Log/Writer/Db.php new file mode 100644 index 000000000..58d09bb37 --- /dev/null +++ b/thirdparty/Zend/Log/Writer/Db.php @@ -0,0 +1,113 @@ +_db = $db; + $this->_table = $table; + $this->_columnMap = $columnMap; + } + + /** + * Formatting is not possible on this writer + */ + public function setFormatter($formatter) + { + require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception(get_class() . ' does not support formatting'); + } + + /** + * Remove reference to database adapter + * + * @return void + */ + public function shutdown() + { + $this->_db = null; + } + + /** + * Write a message to the log. + * + * @param array $event event data + * @return void + */ + protected function _write($event) + { + if ($this->_db === null) { + require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Database adapter is null'); + } + + if ($this->_columnMap === null) { + $dataToInsert = $event; + } else { + $dataToInsert = array(); + foreach ($this->_columnMap as $columnName => $fieldKey) { + $dataToInsert[$columnName] = $event[$fieldKey]; + } + } + + $this->_db->insert($this->_table, $dataToInsert); + } + +} diff --git a/thirdparty/Zend/Log/Writer/Firebug.php b/thirdparty/Zend/Log/Writer/Firebug.php new file mode 100644 index 000000000..c4162f654 --- /dev/null +++ b/thirdparty/Zend/Log/Writer/Firebug.php @@ -0,0 +1,187 @@ + 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/thirdparty/Zend/Log/Writer/Mail.php b/thirdparty/Zend/Log/Writer/Mail.php new file mode 100644 index 000000000..1c49ae2eb --- /dev/null +++ b/thirdparty/Zend/Log/Writer/Mail.php @@ -0,0 +1,278 @@ +_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); + $this->_mail->setBodyHtml($this->_layout->render()); + } + + // Finally, send the mail, but re-throw any exceptions at the + // proper level of abstraction. + try { + $this->_mail->send(); + } catch (Exception $e) { + throw new Zend_Log_Exception( + $e->getMessage(), + $e->getCode()); + } + } + + /** + * 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/thirdparty/Zend/Log/Writer/Mock.php b/thirdparty/Zend/Log/Writer/Mock.php new file mode 100644 index 000000000..cf97a5a24 --- /dev/null +++ b/thirdparty/Zend/Log/Writer/Mock.php @@ -0,0 +1,66 @@ +events[] = $event; + } + + /** + * Record shutdown + * + * @return void + */ + public function shutdown() + { + $this->shutdown = true; + } +} \ No newline at end of file diff --git a/thirdparty/Zend/Log/Writer/Null.php b/thirdparty/Zend/Log/Writer/Null.php new file mode 100644 index 000000000..cb44c36c2 --- /dev/null +++ b/thirdparty/Zend/Log/Writer/Null.php @@ -0,0 +1,46 @@ +_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); + } + } + + $this->_formatter = new Zend_Log_Formatter_Simple(); + } + + /** + * Close the stream resource. + * + * @return void + */ + public function shutdown() + { + if (is_resource($this->_stream)) { + fclose($this->_stream); + } + } + + /** + * Write a message to the log. + * + * @param array $event event data + * @return void + */ + protected function _write($event) + { + $line = $this->_formatter->format($event); + + if (false === @fwrite($this->_stream, $line)) { + require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception("Unable to write to stream"); + } + } + +}