PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src-all/piwik/libs/Zend/Log.php

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