From 6dba205e9021d20f3c12fad2450e413ca7054196 Mon Sep 17 00:00:00 2001 From: Maksym Aposov Date: Fri, 16 Sep 2022 09:48:26 -0500 Subject: [PATCH] Initial commit --- .gitignore | 9 + LICENSE.txt | 27 + README.md | 14 + composer.json | 25 + library/Zend/Log.php | 662 +++++++++++++++++++++++ library/Zend/Log/Exception.php | 33 ++ library/Zend/Log/FactoryInterface.php | 38 ++ library/Zend/Log/Filter/Abstract.php | 60 ++ library/Zend/Log/Filter/Interface.php | 40 ++ library/Zend/Log/Filter/Message.php | 85 +++ library/Zend/Log/Filter/Priority.php | 101 ++++ library/Zend/Log/Filter/Suppress.php | 77 +++ library/Zend/Log/Formatter/Abstract.php | 40 ++ library/Zend/Log/Formatter/Interface.php | 41 ++ library/Zend/Log/Formatter/Simple.php | 108 ++++ library/Zend/Log/Formatter/Xml.php | 165 ++++++ library/Zend/Log/Writer/Abstract.php | 141 +++++ library/Zend/Log/Writer/Db.php | 147 +++++ library/Zend/Log/Writer/Mock.php | 81 +++ library/Zend/Log/Writer/Null.php | 56 ++ library/Zend/Log/Writer/Stream.php | 138 +++++ library/Zend/Log/Writer/Syslog.php | 267 +++++++++ library/Zend/Log/Writer/ZendMonitor.php | 131 +++++ 23 files changed, 2486 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 composer.json create mode 100644 library/Zend/Log.php create mode 100644 library/Zend/Log/Exception.php create mode 100644 library/Zend/Log/FactoryInterface.php create mode 100644 library/Zend/Log/Filter/Abstract.php create mode 100644 library/Zend/Log/Filter/Interface.php create mode 100644 library/Zend/Log/Filter/Message.php create mode 100644 library/Zend/Log/Filter/Priority.php create mode 100644 library/Zend/Log/Filter/Suppress.php create mode 100644 library/Zend/Log/Formatter/Abstract.php create mode 100644 library/Zend/Log/Formatter/Interface.php create mode 100644 library/Zend/Log/Formatter/Simple.php create mode 100644 library/Zend/Log/Formatter/Xml.php create mode 100644 library/Zend/Log/Writer/Abstract.php create mode 100644 library/Zend/Log/Writer/Db.php create mode 100644 library/Zend/Log/Writer/Mock.php create mode 100644 library/Zend/Log/Writer/Null.php create mode 100644 library/Zend/Log/Writer/Stream.php create mode 100644 library/Zend/Log/Writer/Syslog.php create mode 100644 library/Zend/Log/Writer/ZendMonitor.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..27b6bf0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/.buildpath +/.cache +/.metadata +/.project +/.settings +/.vscode +/.idea +/.gitattributes +.DS_Store diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..6eab5aa --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,27 @@ +Copyright (c) 2005-2015, Zend Technologies USA, Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of Zend Technologies USA, Inc. nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3e03c69 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +INFORMATION +=================== + +This is a fork from Zend Framework 1.12.16 Release. + +PURPOSE +--------------------------- +This package is a part of the Zend Framework 1. Component was separated and put into its own composer package. + +LICENSE +======= + +The files in this archive are released under the Zend Framework license. +You can find a copy of this license in [LICENSE.txt](LICENSE.txt). diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3e77467 --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "magento/zend-log", + "description": "Magento Zend Framework 1", + "type": "library", + "keywords": [ + "framework", + "zf1" + ], + "homepage": "http://framework.zend.com/", + "license": "BSD-3-Clause", + "require": { + "php": ">=7.0.0", + "magento/zend-exception": "^1.16" + }, + "autoload": { + "psr-0": { + "Zend_Log": "library/" + } + }, + "extra": { + "branch-alias": { + "dev-main": "1.16.x-dev" + } + } +} diff --git a/library/Zend/Log.php b/library/Zend/Log.php new file mode 100644 index 0000000..facd73e --- /dev/null +++ b/library/Zend/Log.php @@ -0,0 +1,662 @@ +_priorities = array_flip($r->getConstants()); + + if ($writer !== null) { + $this->addWriter($writer); + } + } + + /** + * Factory to construct the logger and one or more writers + * based on the configuration array + * + * @param array|Zend_Config Array or instance of Zend_Config + * @return Zend_Log + * @throws Zend_Log_Exception + */ + static public function factory($config = array()) + { + if ($config instanceof Zend_Config) { + $config = $config->toArray(); + } + + if (!is_array($config) || empty($config)) { + /** @see Zend_Log_Exception */ + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Configuration must be an array or instance of Zend_Config'); + } + + if (array_key_exists('className', $config)) { + $class = $config['className']; + unset($config['className']); + } else { + $class = __CLASS__; + } + + $log = new $class; + + if (!$log instanceof Zend_Log) { + /** @see Zend_Log_Exception */ + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Passed className does not belong to a descendant of Zend_Log'); + } + + if (array_key_exists('timestampFormat', $config)) { + if (null != $config['timestampFormat'] && '' != $config['timestampFormat']) { + $log->setTimestampFormat($config['timestampFormat']); + } + unset($config['timestampFormat']); + } + + if (!is_array(current($config))) { + $log->addWriter(current($config)); + } else { + foreach($config as $writer) { + $log->addWriter($writer); + } + } + + return $log; + } + + + /** + * Construct a writer object based on a configuration array + * + * @param array $config config array with writer spec + * @return Zend_Log_Writer_Abstract + * @throws Zend_Log_Exception + */ + protected function _constructWriterFromConfig($config) + { + $writer = $this->_constructFromConfig('writer', $config, $this->_defaultWriterNamespace); + + if (!$writer instanceof Zend_Log_Writer_Abstract) { + $writerName = is_object($writer) + ? get_class($writer) + : 'The specified writer'; + /** @see Zend_Log_Exception */ + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception("{$writerName} does not extend Zend_Log_Writer_Abstract!"); + } + + if (isset($config['filterName'])) { + $filter = $this->_constructFilterFromConfig($config); + $writer->addFilter($filter); + } + + if (isset($config['formatterName'])) { + $formatter = $this->_constructFormatterFromConfig($config); + $writer->setFormatter($formatter); + } + + return $writer; + } + + /** + * Construct filter object from configuration array or Zend_Config object + * + * @param array|Zend_Config $config Zend_Config or Array + * @return Zend_Log_Filter_Interface + * @throws Zend_Log_Exception + */ + protected function _constructFilterFromConfig($config) + { + $filter = $this->_constructFromConfig('filter', $config, $this->_defaultFilterNamespace); + + if (!$filter instanceof Zend_Log_Filter_Interface) { + $filterName = is_object($filter) + ? get_class($filter) + : 'The specified filter'; + /** @see Zend_Log_Exception */ + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception("{$filterName} does not implement Zend_Log_Filter_Interface"); + } + + return $filter; + } + + /** + * Construct formatter object from configuration array or Zend_Config object + * + * @param array|Zend_Config $config Zend_Config or Array + * @return Zend_Log_Formatter_Interface + * @throws Zend_Log_Exception + */ + protected function _constructFormatterFromConfig($config) + { + $formatter = $this->_constructFromConfig('formatter', $config, $this->_defaultFormatterNamespace); + + if (!$formatter instanceof Zend_Log_Formatter_Interface) { + $formatterName = is_object($formatter) + ? get_class($formatter) + : 'The specified formatter'; + /** @see Zend_Log_Exception */ + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception($formatterName . ' does not implement Zend_Log_Formatter_Interface'); + } + + return $formatter; + } + + /** + * Construct a filter or writer from config + * + * @param string $type 'writer' of 'filter' + * @param mixed $config Zend_Config or Array + * @param string $namespace + * @return object + * @throws Zend_Log_Exception + */ + protected function _constructFromConfig($type, $config, $namespace) + { + if ($config instanceof Zend_Config) { + $config = $config->toArray(); + } + + if (!is_array($config) || empty($config)) { + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception( + 'Configuration must be an array or instance of Zend_Config' + ); + } + + $params = isset($config[ $type .'Params' ]) ? $config[ $type .'Params' ] : array(); + $className = $this->getClassName($config, $type, $namespace); + if (!class_exists($className)) { + #require_once 'Zend/Loader.php'; + Zend_Loader::loadClass($className); + } + + $reflection = new ReflectionClass($className); + if (!$reflection->implementsInterface('Zend_Log_FactoryInterface')) { + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception( + $className . ' does not implement Zend_Log_FactoryInterface and can not be constructed from config.' + ); + } + + return call_user_func(array($className, 'factory'), $params); + } + + /** + * Get the writer or filter full classname + * + * @param array $config + * @param string $type filter|writer + * @param string $defaultNamespace + * @return string full classname + * @throws Zend_Log_Exception + */ + protected function getClassName($config, $type, $defaultNamespace) + { + if (!isset($config[$type . 'Name'])) { + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception("Specify {$type}Name in the configuration array"); + } + + $className = $config[$type . 'Name']; + $namespace = $defaultNamespace; + + if (isset($config[$type . 'Namespace'])) { + $namespace = $config[$type . 'Namespace']; + } + + // PHP >= 5.3.0 namespace given? + if (substr($namespace, -1) == '\\') { + return $namespace . $className; + } + + // empty namespace given? + if (strlen($namespace) === 0) { + return $className; + } + + return $namespace . '_' . $className; + } + + /** + * Packs message and priority into Event array + * + * @param string $message Message to log + * @param integer $priority Priority of message + * @return array Event array + */ + protected function _packEvent($message, $priority) + { + return array_merge(array( + 'timestamp' => date($this->_timestampFormat), + 'message' => $message, + 'priority' => $priority, + 'priorityName' => $this->_priorities[$priority] + ), + $this->_extras + ); + } + + /** + * Class destructor. Shutdown log writers + * + * @return void + */ + public function __destruct() + { + /** @var Zend_Log_Writer_Abstract $writer */ + 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) { + switch (count($params)) { + case 0: + /** @see Zend_Log_Exception */ + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Missing log message'); + case 1: + $message = array_shift($params); + $extras = null; + break; + default: + $message = array_shift($params); + $extras = array_shift($params); + break; + } + $this->log($message, $priority, $extras); + } 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 + * @param mixed $extras Extra information to log in event + * @return void + * @throws Zend_Log_Exception + */ + public function log($message, $priority, $extras = null) + { + // 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 = $this->_packEvent($message, $priority); + + // Check to see if any extra information was passed + if (!empty($extras)) { + $info = array(); + if (is_array($extras)) { + foreach ($extras as $key => $value) { + if (is_string($key)) { + $event[$key] = $value; + } else { + $info[] = $value; + } + } + } else { + $info = $extras; + } + if (!empty($info)) { + $event['info'] = $info; + } + } + + // abort if rejected by the global filters + /** @var Zend_Log_Filter_Interface $filter */ + foreach ($this->_filters as $filter) { + if (! $filter->accept($event)) { + return; + } + } + + // send to each writer + /** @var Zend_Log_Writer_Abstract $writer */ + foreach ($this->_writers as $writer) { + $writer->write($event); + } + } + + /** + * Add a custom priority + * + * @param string $name Name of priority + * @param integer $priority Numeric priority + * @return $this + * @throws Zend_Log_Exception + */ + public function addPriority($name, $priority) + { + // Priority names must be uppercase for predictability. + $name = strtoupper($name); + + if (isset($this->_priorities[$priority]) + || false !== 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; + return $this; + } + + /** + * 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_Config|array|Zend_Log_Filter_Interface $filter + * @return $this + * @throws Zend_Log_Exception + */ + public function addFilter($filter) + { + if (is_int($filter)) { + /** @see Zend_Log_Filter_Priority */ + #require_once 'Zend/Log/Filter/Priority.php'; + $filter = new Zend_Log_Filter_Priority($filter); + + } elseif ($filter instanceof Zend_Config || is_array($filter)) { + $filter = $this->_constructFilterFromConfig($filter); + + } elseif(! $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; + return $this; + } + + /** + * Add a writer. A writer is responsible for taking a log + * message and writing it out to storage. + * + * @param mixed $writer Zend_Log_Writer_Abstract or Config array + * @return Zend_Log + * @throws Zend_Log_Exception + */ + public function addWriter($writer) + { + if (is_array($writer) || $writer instanceof Zend_Config) { + $writer = $this->_constructWriterFromConfig($writer); + } + + if (!$writer instanceof Zend_Log_Writer_Abstract) { + /** @see Zend_Log_Exception */ + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception( + 'Writer must be an instance of Zend_Log_Writer_Abstract' + . ' or you should pass a configuration array' + ); + } + + $this->_writers[] = $writer; + return $this; + } + + /** + * Set an extra item to pass to the log writers. + * + * @param string $name Name of the field + * @param string $value Value of the field + * @return Zend_Log + */ + public function setEventItem($name, $value) + { + $this->_extras = array_merge($this->_extras, array($name => $value)); + return $this; + } + + /** + * Register Logging system as an error handler to log php errors + * Note: it still calls the original error handler if set_error_handler is able to return it. + * + * Errors will be mapped as: + * E_NOTICE, E_USER_NOTICE => NOTICE + * E_WARNING, E_CORE_WARNING, E_USER_WARNING => WARN + * E_ERROR, E_USER_ERROR, E_CORE_ERROR, E_RECOVERABLE_ERROR => ERR + * E_DEPRECATED, E_STRICT, E_USER_DEPRECATED => DEBUG + * (unknown/other) => INFO + * + * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler + * + * @return Zend_Log + */ + public function registerErrorHandler() + { + // Only register once. Avoids loop issues if it gets registered twice. + if ($this->_registeredErrorHandler) { + return $this; + } + + $this->_origErrorHandler = set_error_handler(array($this, 'errorHandler')); + + // Contruct a default map of phpErrors to Zend_Log priorities. + // Some of the errors are uncatchable, but are included for completeness + $this->_errorHandlerMap = array( + E_NOTICE => Zend_Log::NOTICE, + E_USER_NOTICE => Zend_Log::NOTICE, + E_WARNING => Zend_Log::WARN, + E_CORE_WARNING => Zend_Log::WARN, + E_USER_WARNING => Zend_Log::WARN, + E_ERROR => Zend_Log::ERR, + E_USER_ERROR => Zend_Log::ERR, + E_CORE_ERROR => Zend_Log::ERR, + E_RECOVERABLE_ERROR => Zend_Log::ERR, + E_STRICT => Zend_Log::DEBUG, + ); + // PHP 5.3.0+ + if (defined('E_DEPRECATED')) { + $this->_errorHandlerMap['E_DEPRECATED'] = Zend_Log::DEBUG; + } + if (defined('E_USER_DEPRECATED')) { + $this->_errorHandlerMap['E_USER_DEPRECATED'] = Zend_Log::DEBUG; + } + + $this->_registeredErrorHandler = true; + return $this; + } + + /** + * Error Handler will convert error into log message, and then call the original error handler + * + * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler + * @param int $errno + * @param string $errstr + * @param string $errfile + * @param int $errline + * @param array $errcontext + * @return boolean + */ + public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) + { + $errorLevel = error_reporting(); + + if ($errorLevel & $errno) { + if (isset($this->_errorHandlerMap[$errno])) { + $priority = $this->_errorHandlerMap[$errno]; + } else { + $priority = Zend_Log::INFO; + } + $this->log($errstr, $priority, array('errno'=>$errno, 'file'=>$errfile, 'line'=>$errline, 'context'=>$errcontext)); + } + + if ($this->_origErrorHandler !== null) { + return call_user_func($this->_origErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext); + } + return false; + } + + /** + * Set timestamp format for log entries. + * + * @param string $format + * @return Zend_Log + */ + public function setTimestampFormat($format) + { + $this->_timestampFormat = $format; + return $this; + } + + /** + * Get timestamp format used for log entries. + * + * @return string + */ + public function getTimestampFormat() + { + return $this->_timestampFormat; + } +} diff --git a/library/Zend/Log/Exception.php b/library/Zend/Log/Exception.php new file mode 100644 index 0000000..775ca7c --- /dev/null +++ b/library/Zend/Log/Exception.php @@ -0,0 +1,33 @@ +toArray(); + } + + if (!is_array($config)) { + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Configuration must be an array or instance of Zend_Config'); + } + + return $config; + } +} diff --git a/library/Zend/Log/Filter/Interface.php b/library/Zend/Log/Filter/Interface.php new file mode 100644 index 0000000..dc0cbad --- /dev/null +++ b/library/Zend/Log/Filter/Interface.php @@ -0,0 +1,40 @@ +_regexp = $regexp; + } + + /** + * Create a new instance of Zend_Log_Filter_Message + * + * @param array|Zend_Config $config + * @return Zend_Log_Filter_Message + */ + static public function factory($config) + { + $config = self::_parseConfig($config); + $config = array_merge(array( + 'regexp' => null + ), $config); + + return new self( + $config['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/library/Zend/Log/Filter/Priority.php b/library/Zend/Log/Filter/Priority.php new file mode 100644 index 0000000..21bbc82 --- /dev/null +++ b/library/Zend/Log/Filter/Priority.php @@ -0,0 +1,101 @@ +_priority = $priority; + $this->_operator = $operator === null ? '<=' : $operator; + } + + /** + * Create a new instance of Zend_Log_Filter_Priority + * + * @param array|Zend_Config $config + * @return Zend_Log_Filter_Priority + */ + static public function factory($config) + { + $config = self::_parseConfig($config); + $config = array_merge(array( + 'priority' => null, + 'operator' => null, + ), $config); + + // Add support for constants + if (!is_numeric($config['priority']) && isset($config['priority']) && defined($config['priority'])) { + $config['priority'] = constant($config['priority']); + } + + return new self( + (int) $config['priority'], + $config['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/library/Zend/Log/Filter/Suppress.php b/library/Zend/Log/Filter/Suppress.php new file mode 100644 index 0000000..5d288aa --- /dev/null +++ b/library/Zend/Log/Filter/Suppress.php @@ -0,0 +1,77 @@ +_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; + } + + /** + * Create a new instance of Zend_Log_Filter_Suppress + * + * @param array|Zend_Config $config + * @return Zend_Log_Filter_Suppress + * @throws Zend_Log_Exception + */ + static public function factory($config) + { + return new self(); + } +} diff --git a/library/Zend/Log/Formatter/Abstract.php b/library/Zend/Log/Formatter/Abstract.php new file mode 100644 index 0000000..caa1f3e --- /dev/null +++ b/library/Zend/Log/Formatter/Abstract.php @@ -0,0 +1,40 @@ +_format = $format; + } + + /** + * Factory for Zend_Log_Formatter_Simple classe + * + * @param array|Zend_Config $options + * @return Zend_Log_Formatter_Simple + */ + public static function factory($options) + { + $format = null; + if (null !== $options) { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + + if (array_key_exists('format', $options)) { + $format = $options['format']; + } + } + + return new self($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/library/Zend/Log/Formatter/Xml.php b/library/Zend/Log/Formatter/Xml.php new file mode 100644 index 0000000..039bd4c --- /dev/null +++ b/library/Zend/Log/Formatter/Xml.php @@ -0,0 +1,165 @@ +toArray(); + } elseif (!is_array($options)) { + $args = func_get_args(); + + $options = array( + 'rootElement' => array_shift($args) + ); + + if (count($args)) { + $options['elementMap'] = array_shift($args); + } + + if (count($args)) { + $options['encoding'] = array_shift($args); + } + } + + if (!array_key_exists('rootElement', $options)) { + $options['rootElement'] = 'logEntry'; + } + + if (!array_key_exists('encoding', $options)) { + $options['encoding'] = 'UTF-8'; + } + + $this->_rootElement = $options['rootElement']; + $this->setEncoding($options['encoding']); + + if (array_key_exists('elementMap', $options)) { + $this->_elementMap = $options['elementMap']; + } + } + + /** + * Factory for Zend_Log_Formatter_Xml classe + * + * @param array|Zend_Config $options + * @return Zend_Log_Formatter_Xml + */ + public static function factory($options) + { + return new self($options); + } + + /** + * Get encoding + * + * @return string + */ + public function getEncoding() + { + return $this->_encoding; + } + + /** + * Set encoding + * + * @param string $value + * @return Zend_Log_Formatter_Xml + */ + public function setEncoding($value) + { + $this->_encoding = (string) $value; + return $this; + } + + /** + * 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]; + } + } + + $enc = $this->getEncoding(); + $dom = new DOMDocument('1.0', $enc); + $elt = $dom->appendChild(new DOMElement($this->_rootElement)); + + foreach ($dataToInsert as $key => $value) { + if (empty($value) + || is_scalar($value) + || (is_object($value) && method_exists($value,'__toString')) + ) { + if($key == "message") { + $value = htmlspecialchars($value, ENT_COMPAT, $enc); + } + $elt->appendChild(new DOMElement($key, (string)$value)); + } + } + + $xml = $dom->saveXML(); + $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml); + + return $xml . PHP_EOL; + } +} diff --git a/library/Zend/Log/Writer/Abstract.php b/library/Zend/Log/Writer/Abstract.php new file mode 100644 index 0000000..f9934ef --- /dev/null +++ b/library/Zend/Log/Writer/Abstract.php @@ -0,0 +1,141 @@ +_filters[] = $filter; + return $this; + } + + /** + * Log a message to this writer. + * + * @param array $event log data event + * @return void + */ + public function write($event) + { + /** @var Zend_Log_Filter_Interface $filter */ + 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 Zend_Log_Writer_Abstract + */ + public function setFormatter(Zend_Log_Formatter_Interface $formatter) + { + $this->_formatter = $formatter; + return $this; + } + + /** + * 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); + + /** + * Validate and optionally convert the config to array + * + * @param array|Zend_Config $config Zend_Config or Array + * @return array + * @throws Zend_Log_Exception + */ + static protected function _parseConfig($config) + { + if ($config instanceof Zend_Config) { + $config = $config->toArray(); + } + + if (!is_array($config)) { + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception( + 'Configuration must be an array or instance of Zend_Config' + ); + } + + return $config; + } +} diff --git a/library/Zend/Log/Writer/Db.php b/library/Zend/Log/Writer/Db.php new file mode 100644 index 0000000..2511a65 --- /dev/null +++ b/library/Zend/Log/Writer/Db.php @@ -0,0 +1,147 @@ +_db = $db; + $this->_table = $table; + $this->_columnMap = $columnMap; + } + + /** + * Create a new instance of Zend_Log_Writer_Db + * + * @param array|Zend_Config $config + * @return Zend_Log_Writer_Db + */ + static public function factory($config) + { + $config = self::_parseConfig($config); + $config = array_merge(array( + 'db' => null, + 'table' => null, + 'columnMap' => null, + ), $config); + + if (isset($config['columnmap'])) { + $config['columnMap'] = $config['columnmap']; + } + + return new self( + $config['db'], + $config['table'], + $config['columnMap'] + ); + } + + /** + * Formatting is not possible on this writer + * + * @return void + * @throws Zend_Log_Exception + */ + public function setFormatter(Zend_Log_Formatter_Interface $formatter) + { + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception(get_class($this) . ' 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 + * @throws Zend_Log_Exception + */ + 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) { + if (isset($event[$fieldKey])) { + $dataToInsert[$columnName] = $event[$fieldKey]; + } + } + } + + $this->_db->insert($this->_table, $dataToInsert); + } +} diff --git a/library/Zend/Log/Writer/Mock.php b/library/Zend/Log/Writer/Mock.php new file mode 100644 index 0000000..a365339 --- /dev/null +++ b/library/Zend/Log/Writer/Mock.php @@ -0,0 +1,81 @@ +events[] = $event; + } + + /** + * Record shutdown + * + * @return void + */ + public function shutdown() + { + $this->shutdown = true; + } + + /** + * Create a new instance of Zend_Log_Writer_Mock + * + * @param array|Zend_Config $config + * @return Zend_Log_Writer_Mock + */ + static public function factory($config) + { + return new self(); + } +} diff --git a/library/Zend/Log/Writer/Null.php b/library/Zend/Log/Writer/Null.php new file mode 100644 index 0000000..f6b2184 --- /dev/null +++ b/library/Zend/Log/Writer/Null.php @@ -0,0 +1,56 @@ +_stream = $streamOrUrl; + } else { + if (is_array($streamOrUrl) && isset($streamOrUrl['stream'])) { + $streamOrUrl = $streamOrUrl['stream']; + } + + 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(); + } + + /** + * Create a new instance of Zend_Log_Writer_Stream + * + * @param array|Zend_Config $config + * @return Zend_Log_Writer_Stream + */ + static public function factory($config) + { + $config = self::_parseConfig($config); + $config = array_merge(array( + 'stream' => null, + 'mode' => null, + ), $config); + + $streamOrUrl = isset($config['url']) ? $config['url'] : $config['stream']; + + return new self( + $streamOrUrl, + $config['mode'] + ); + } + + /** + * 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 + * @throws Zend_Log_Exception + */ + 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"); + } + } +} diff --git a/library/Zend/Log/Writer/Syslog.php b/library/Zend/Log/Writer/Syslog.php new file mode 100644 index 0000000..f361ef0 --- /dev/null +++ b/library/Zend/Log/Writer/Syslog.php @@ -0,0 +1,267 @@ + 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 int + */ + protected $_facility = LOG_USER; + + /** + * Types of program available to logging of message + * + * @var array + */ + protected $_validFacilities = array(); + + /** + * Class constructor + * + * @param array $params 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']; + } + + $runInitializeSyslog = true; + if (isset($params['facility'])) { + $this->setFacility($params['facility']); + $runInitializeSyslog = false; + } + + if ($runInitializeSyslog) { + $this->_initializeSyslog(); + } + } + + /** + * Create a new instance of Zend_Log_Writer_Syslog + * + * @param array|Zend_Config $config + * @return Zend_Log_Writer_Syslog + */ + static public function factory($config) + { + return new self(self::_parseConfig($config)); + } + + /** + * Initialize values facilities + * + * @return void + */ + protected function _initializeValidFacilities() + { + $constants = array( + 'LOG_AUTH', + 'LOG_AUTHPRIV', + 'LOG_CRON', + 'LOG_DAEMON', + 'LOG_KERN', + 'LOG_LOCAL0', + 'LOG_LOCAL1', + 'LOG_LOCAL2', + 'LOG_LOCAL3', + 'LOG_LOCAL4', + 'LOG_LOCAL5', + 'LOG_LOCAL6', + 'LOG_LOCAL7', + 'LOG_LPR', + 'LOG_MAIL', + 'LOG_NEWS', + 'LOG_SYSLOG', + 'LOG_USER', + 'LOG_UUCP' + ); + + foreach ($constants as $constant) { + if (defined($constant)) { + $this->_validFacilities[] = constant($constant); + } + } + } + + /** + * Initialize syslog / set application name and 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 int $facility Syslog facility + * @return Zend_Log_Writer_Syslog + * @throws Zend_Log_Exception for invalid log facility + */ + public function setFacility($facility) + { + if ($this->_facility === $facility) { + return $this; + } + + if (!count($this->_validFacilities)) { + $this->_initializeValidFacilities(); + } + + if (!in_array($facility, $this->_validFacilities)) { + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Invalid log facility provided; please see http://php.net/openlog for a list of valid facility values'); + } + + if ('WIN' == strtoupper(substr(PHP_OS, 0, 3)) + && ($facility !== LOG_USER) + ) { + #require_once 'Zend/Log/Exception.php'; + throw new Zend_Log_Exception('Only LOG_USER is a valid log facility on Windows'); + } + + $this->_facility = $facility; + $this->_initializeSyslog(); + return $this; + } + + /** + * Set application name + * + * @param string $application Application name + * @return Zend_Log_Writer_Syslog + */ + public function setApplicationName($application) + { + if ($this->_application === $application) { + return $this; + } + $this->_application = $application; + $this->_initializeSyslog(); + return $this; + } + + /** + * 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(); + } + + $message = $event['message']; + if ($this->_formatter instanceof Zend_Log_Formatter_Interface) { + $message = $this->_formatter->format($event); + } + + syslog($priority, $message); + } +} diff --git a/library/Zend/Log/Writer/ZendMonitor.php b/library/Zend/Log/Writer/ZendMonitor.php new file mode 100644 index 0000000..fdc8d83 --- /dev/null +++ b/library/Zend/Log/Writer/ZendMonitor.php @@ -0,0 +1,131 @@ +_isEnabled = false; + } + if (function_exists('zend_monitor_custom_event')) { + $this->_isZendServer = true; + } + } + + /** + * Create a new instance of Zend_Log_Writer_ZendMonitor + * + * @param array|Zend_Config $config + * @return Zend_Log_Writer_ZendMonitor + */ + static public function factory($config) + { + return new self(); + } + + /** + * Is logging to this writer enabled? + * + * If the Zend Monitor extension is not enabled, this log writer will + * fail silently. You can query this method to determine if the log + * writer is enabled. + * + * @return boolean + */ + public function isEnabled() + { + return $this->_isEnabled; + } + + /** + * Log a message to this writer. + * + * @param array $event log data event + * @return void + */ + public function write($event) + { + if (!$this->isEnabled()) { + return; + } + + parent::write($event); + } + + /** + * Write a message to the log. + * + * @param array $event log data event + * @return void + */ + protected function _write($event) + { + $priority = $event['priority']; + $message = $event['message']; + unset($event['priority'], $event['message']); + + if (!empty($event)) { + if ($this->_isZendServer) { + // On Zend Server; third argument should be the event + zend_monitor_custom_event($priority, $message, $event); + } else { + // On Zend Platform; third argument is severity -- either + // 0 or 1 -- and fourth is optional (event) + // Severity is either 0 (normal) or 1 (severe); classifying + // notice, info, and debug as "normal", and all others as + // "severe" + monitor_custom_event($priority, $message, ($priority > 4) ? 0 : 1, $event); + } + } else { + monitor_custom_event($priority, $message); + } + } +}