Skip to content
This repository
Browse code

committed missing log lib

  • Loading branch information...
commit 5d9af1999c6b9d3148eee632979d71f5f6f57de1 1 parent 3faf0f6
Demian Turner authored

Showing 1 changed file with 425 additions and 0 deletions. Show diff stats Hide diff stats

  1. +425 0 src/lib/Zend/Log.php
425 src/lib/Zend/Log.php
... ... @@ -0,0 +1,425 @@
  1 +<?php
  2 +/**
  3 + * Zend Framework
  4 + *
  5 + * LICENSE
  6 + *
  7 + * This source file is subject to the new BSD license that is bundled
  8 + * with this package in the file LICENSE.txt.
  9 + * It is also available through the world-wide-web at this URL:
  10 + * http://framework.zend.com/license/new-bsd
  11 + * If you did not receive a copy of the license and are unable to
  12 + * obtain it through the world-wide-web, please send an email
  13 + * to license@zend.com so we can send you a copy immediately.
  14 + *
  15 + * @category Zend
  16 + * @package Zend_Log
  17 + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18 + * @license http://framework.zend.com/license/new-bsd New BSD License
  19 + * @version $Id: Log.php 20893 2010-02-03 22:59:25Z yoshida@zend.co.jp $
  20 + */
  21 +
  22 +/**
  23 + * @category Zend
  24 + * @package Zend_Log
  25 + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  26 + * @license http://framework.zend.com/license/new-bsd New BSD License
  27 + * @version $Id: Log.php 20893 2010-02-03 22:59:25Z yoshida@zend.co.jp $
  28 + */
  29 +class Zend_Log
  30 +{
  31 + const EMERG = 0; // Emergency: system is unusable
  32 + const ALERT = 1; // Alert: action must be taken immediately
  33 + const CRIT = 2; // Critical: critical conditions
  34 + const ERR = 3; // Error: error conditions
  35 + const WARN = 4; // Warning: warning conditions
  36 + const NOTICE = 5; // Notice: normal but significant condition
  37 + const INFO = 6; // Informational: informational messages
  38 + const DEBUG = 7; // Debug: debug messages
  39 +
  40 + /**
  41 + * @var array of priorities where the keys are the
  42 + * priority numbers and the values are the priority names
  43 + */
  44 + protected $_priorities = array();
  45 +
  46 + /**
  47 + * @var array of Zend_Log_Writer_Abstract
  48 + */
  49 + protected $_writers = array();
  50 +
  51 + /**
  52 + * @var array of Zend_Log_Filter_Interface
  53 + */
  54 + protected $_filters = array();
  55 +
  56 + /**
  57 + * @var array of extra log event
  58 + */
  59 + protected $_extras = array();
  60 +
  61 + /**
  62 + *
  63 + * @var string
  64 + */
  65 + protected $_defaultWriterNamespace = 'Zend_Log_Writer';
  66 +
  67 + /**
  68 + *
  69 + * @var string
  70 + */
  71 + protected $_defaultFilterNamespace = 'Zend_Log_Filter';
  72 +
  73 +
  74 + /**
  75 + * Class constructor. Create a new logger
  76 + *
  77 + * @param Zend_Log_Writer_Abstract|null $writer default writer
  78 + */
  79 + public function __construct(Zend_Log_Writer_Abstract $writer = null)
  80 + {
  81 + $r = new ReflectionClass($this);
  82 + $this->_priorities = array_flip($r->getConstants());
  83 +
  84 + if ($writer !== null) {
  85 + $this->addWriter($writer);
  86 + }
  87 + }
  88 +
  89 + /**
  90 + * Factory to construct the logger and one or more writers
  91 + * based on the configuration array
  92 + *
  93 + * @param array|Zend_Config Array or instance of Zend_Config
  94 + * @return Zend_Log
  95 + */
  96 + static public function factory($config = array())
  97 + {
  98 + if ($config instanceof Zend_Config) {
  99 + $config = $config->toArray();
  100 + }
  101 +
  102 + if (!is_array($config) || empty($config)) {
  103 + /** @see Zend_Log_Exception */
  104 + require_once 'Zend/Log/Exception.php';
  105 + throw new Zend_Log_Exception('Configuration must be an array or instance of Zend_Config');
  106 + }
  107 +
  108 + $log = new Zend_Log;
  109 +
  110 + if (!is_array(current($config))) {
  111 + $log->addWriter(current($config));
  112 + } else {
  113 + foreach($config as $writer) {
  114 + $log->addWriter($writer);
  115 + }
  116 + }
  117 +
  118 + return $log;
  119 + }
  120 +
  121 +
  122 + /**
  123 + * Construct a writer object based on a configuration array
  124 + *
  125 + * @param array $spec config array with writer spec
  126 + * @return Zend_Log_Writer_Abstract
  127 + */
  128 + protected function _constructWriterFromConfig($config)
  129 + {
  130 + $writer = $this->_constructFromConfig('writer', $config, $this->_defaultWriterNamespace);
  131 +
  132 + if (!$writer instanceof Zend_Log_Writer_Abstract) {
  133 + /** @see Zend_Log_Exception */
  134 + require_once 'Zend/Log/Exception.php';
  135 + throw new Zend_Log_Exception("{$writerName} does not extend Zend_Log_Writer_Abstract!");
  136 + }
  137 +
  138 + if (isset($config['filterName'])) {
  139 + $filter = $this->_constructFilterFromConfig($config);
  140 + $writer->addFilter($filter);
  141 + }
  142 +
  143 + return $writer;
  144 + }
  145 +
  146 + /**
  147 + * Construct filter object from configuration array or Zend_Config object
  148 + *
  149 + * @param array|Zend_Config $config Zend_Config or Array
  150 + * @return Zend_Log_Filter_Interface
  151 + */
  152 + protected function _constructFilterFromConfig($config)
  153 + {
  154 + $filter = $this->_constructFromConfig('filter', $config, $this->_defaultFilterNamespace);
  155 +
  156 + if (!$filter instanceof Zend_Log_Filter_Interface) {
  157 + /** @see Zend_Log_Exception */
  158 + require_once 'Zend/Log/Exception.php';
  159 + throw new Zend_Log_Exception("{$filterName} does not implement Zend_Log_Filter_Interface");
  160 + }
  161 +
  162 + return $filter;
  163 + }
  164 +
  165 + /**
  166 + * Construct a filter or writer from config
  167 + *
  168 + * @param string $type 'writer' of 'filter'
  169 + * @param mixed $config Zend_Config or Array
  170 + * @param string $namespace
  171 + * @return object
  172 + */
  173 + protected function _constructFromConfig($type, $config, $namespace)
  174 + {
  175 + if ($config instanceof Zend_Config) {
  176 + $config = $config->toArray();
  177 + }
  178 +
  179 + if (!is_array($config) || empty($config)) {
  180 + require_once 'Zend/Log/Exception.php';
  181 + throw new Zend_Log_Exception(
  182 + 'Configuration must be an array or instance of Zend_Config'
  183 + );
  184 + }
  185 +
  186 + $params = isset($config[ $type .'Params' ]) ? $config[ $type .'Params' ] : array();
  187 + $className = $this->getClassName($config, $type, $namespace);
  188 + if (!class_exists($className)) {
  189 + require_once 'Zend/Loader.php';
  190 + Zend_Loader::loadClass($className);
  191 + }
  192 +
  193 + $reflection = new ReflectionClass($className);
  194 + if (!$reflection->implementsInterface('Zend_Log_FactoryInterface')) {
  195 + require_once 'Zend/Log/Exception.php';
  196 + throw new Zend_Log_Exception(
  197 + 'Driver does not implement Zend_Log_FactoryInterface and can not be constructed from config.'
  198 + );
  199 + }
  200 +
  201 + return call_user_func(array($className, 'factory'), $params);
  202 + }
  203 +
  204 + /**
  205 + * Get the writer or filter full classname
  206 + *
  207 + * @param array $config
  208 + * @param string $type filter|writer
  209 + * @param string $defaultNamespace
  210 + * @return string full classname
  211 + */
  212 + protected function getClassName($config, $type, $defaultNamespace)
  213 + {
  214 + if (!isset($config[ $type . 'Name' ])) {
  215 + require_once 'Zend/Log/Exception.php';
  216 + throw new Zend_Log_Exception("Specify {$type}Name in the configuration array");
  217 + }
  218 + $className = $config[ $type . 'Name' ];
  219 +
  220 + $namespace = $defaultNamespace;
  221 + if (isset($config[ $type . 'Namespace' ])) {
  222 + $namespace = $config[ $type . 'Namespace' ];
  223 + }
  224 +
  225 + $fullClassName = $namespace . '_' . $className;
  226 + return $fullClassName;
  227 + }
  228 +
  229 + /**
  230 + * Class destructor. Shutdown log writers
  231 + *
  232 + * @return void
  233 + */
  234 + public function __destruct()
  235 + {
  236 + foreach($this->_writers as $writer) {
  237 + $writer->shutdown();
  238 + }
  239 + }
  240 +
  241 + /**
  242 + * Undefined method handler allows a shortcut:
  243 + * $log->priorityName('message')
  244 + * instead of
  245 + * $log->log('message', Zend_Log::PRIORITY_NAME)
  246 + *
  247 + * @param string $method priority name
  248 + * @param string $params message to log
  249 + * @return void
  250 + * @throws Zend_Log_Exception
  251 + */
  252 + public function __call($method, $params)
  253 + {
  254 + $priority = strtoupper($method);
  255 + if (($priority = array_search($priority, $this->_priorities)) !== false) {
  256 + switch (count($params)) {
  257 + case 0:
  258 + /** @see Zend_Log_Exception */
  259 + require_once 'Zend/Log/Exception.php';
  260 + throw new Zend_Log_Exception('Missing log message');
  261 + case 1:
  262 + $message = array_shift($params);
  263 + $extras = null;
  264 + break;
  265 + default:
  266 + $message = array_shift($params);
  267 + $extras = array_shift($params);
  268 + break;
  269 + }
  270 + $this->log($message, $priority, $extras);
  271 + } else {
  272 + /** @see Zend_Log_Exception */
  273 + require_once 'Zend/Log/Exception.php';
  274 + throw new Zend_Log_Exception('Bad log priority');
  275 + }
  276 + }
  277 +
  278 + /**
  279 + * Log a message at a priority
  280 + *
  281 + * @param string $message Message to log
  282 + * @param integer $priority Priority of message
  283 + * @param mixed $extras Extra information to log in event
  284 + * @return void
  285 + * @throws Zend_Log_Exception
  286 + */
  287 + public function log($message, $priority, $extras = null)
  288 + {
  289 + // sanity checks
  290 + if (empty($this->_writers)) {
  291 + /** @see Zend_Log_Exception */
  292 + require_once 'Zend/Log/Exception.php';
  293 + throw new Zend_Log_Exception('No writers were added');
  294 + }
  295 +
  296 + if (! isset($this->_priorities[$priority])) {
  297 + /** @see Zend_Log_Exception */
  298 + require_once 'Zend/Log/Exception.php';
  299 + throw new Zend_Log_Exception('Bad log priority');
  300 + }
  301 +
  302 + // pack into event required by filters and writers
  303 + $event = array_merge(array('timestamp' => date('c'),
  304 + 'message' => $message,
  305 + 'priority' => $priority,
  306 + 'priorityName' => $this->_priorities[$priority]),
  307 + $this->_extras);
  308 +
  309 + // Check to see if any extra information was passed
  310 + if (!empty($extras)) {
  311 + $info = array();
  312 + if (is_array($extras)) {
  313 + foreach ($extras as $key => $value) {
  314 + if (is_string($key)) {
  315 + $event[$key] = $value;
  316 + } else {
  317 + $info[] = $value;
  318 + }
  319 + }
  320 + } else {
  321 + $info = $extras;
  322 + }
  323 + if (!empty($info)) {
  324 + $event['info'] = $info;
  325 + }
  326 + }
  327 +
  328 + // abort if rejected by the global filters
  329 + foreach ($this->_filters as $filter) {
  330 + if (! $filter->accept($event)) {
  331 + return;
  332 + }
  333 + }
  334 +
  335 + // send to each writer
  336 + foreach ($this->_writers as $writer) {
  337 + $writer->write($event);
  338 + }
  339 + }
  340 +
  341 + /**
  342 + * Add a custom priority
  343 + *
  344 + * @param string $name Name of priority
  345 + * @param integer $priority Numeric priority
  346 + * @throws Zend_Log_InvalidArgumentException
  347 + */
  348 + public function addPriority($name, $priority)
  349 + {
  350 + // Priority names must be uppercase for predictability.
  351 + $name = strtoupper($name);
  352 +
  353 + if (isset($this->_priorities[$priority])
  354 + || array_search($name, $this->_priorities)) {
  355 + /** @see Zend_Log_Exception */
  356 + require_once 'Zend/Log/Exception.php';
  357 + throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
  358 + }
  359 +
  360 + $this->_priorities[$priority] = $name;
  361 + }
  362 +
  363 + /**
  364 + * Add a filter that will be applied before all log writers.
  365 + * Before a message will be received by any of the writers, it
  366 + * must be accepted by all filters added with this method.
  367 + *
  368 + * @param int|Zend_Log_Filter_Interface $filter
  369 + * @return void
  370 + */
  371 + public function addFilter($filter)
  372 + {
  373 + if (is_integer($filter)) {
  374 + /** @see Zend_Log_Filter_Priority */
  375 + require_once 'Zend/Log/Filter/Priority.php';
  376 + $filter = new Zend_Log_Filter_Priority($filter);
  377 +
  378 + } elseif ($filter instanceof Zend_Config || is_array($filter)) {
  379 + $filter = $this->_constructFilterFromConfig($filter);
  380 +
  381 + } elseif(! $filter instanceof Zend_Log_Filter_Interface) {
  382 + /** @see Zend_Log_Exception */
  383 + require_once 'Zend/Log/Exception.php';
  384 + throw new Zend_Log_Exception('Invalid filter provided');
  385 + }
  386 +
  387 + $this->_filters[] = $filter;
  388 + }
  389 +
  390 + /**
  391 + * Add a writer. A writer is responsible for taking a log
  392 + * message and writing it out to storage.
  393 + *
  394 + * @param mixed $writer Zend_Log_Writer_Abstract or Config array
  395 + * @return void
  396 + */
  397 + public function addWriter($writer)
  398 + {
  399 + if (is_array($writer) || $writer instanceof Zend_Config) {
  400 + $writer = $this->_constructWriterFromConfig($writer);
  401 + }
  402 +
  403 + if (!$writer instanceof Zend_Log_Writer_Abstract) {
  404 + /** @see Zend_Log_Exception */
  405 + require_once 'Zend/Log/Exception.php';
  406 + throw new Zend_Log_Exception(
  407 + 'Writer must be an instance of Zend_Log_Writer_Abstract'
  408 + . ' or you should pass a configuration array'
  409 + );
  410 + }
  411 +
  412 + $this->_writers[] = $writer;
  413 + }
  414 +
  415 + /**
  416 + * Set an extra item to pass to the log writers.
  417 + *
  418 + * @param $name Name of the field
  419 + * @param $value Value of the field
  420 + * @return void
  421 + */
  422 + public function setEventItem($name, $value) {
  423 + $this->_extras = array_merge($this->_extras, array($name => $value));
  424 + }
  425 +}

0 comments on commit 5d9af19

Please sign in to comment.
Something went wrong with that request. Please try again.