PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Log.php

http://github.com/centurion-project/Centurion
PHP | 425 lines | 202 code | 50 blank | 173 comment | 35 complexity | 0c71c5d16947f3a3f26759634a9b3787 MD5 | raw file
Possible License(s): BSD-3-Clause
  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$
  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$
  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. //$1 '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. /** @see Zend_Log_Exception */
  116. //$1 'Zend/Log/Exception.php';
  117. throw new Zend_Log_Exception("{$writerName} does not extend Zend_Log_Writer_Abstract!");
  118. }
  119. if (isset($config['filterName'])) {
  120. $filter = $this->_constructFilterFromConfig($config);
  121. $writer->addFilter($filter);
  122. }
  123. return $writer;
  124. }
  125. /**
  126. * Construct filter object from configuration array or Zend_Config object
  127. *
  128. * @param array|Zend_Config $config Zend_Config or Array
  129. * @return Zend_Log_Filter_Interface
  130. */
  131. protected function _constructFilterFromConfig($config)
  132. {
  133. $filter = $this->_constructFromConfig('filter', $config, $this->_defaultFilterNamespace);
  134. if (!$filter instanceof Zend_Log_Filter_Interface) {
  135. /** @see Zend_Log_Exception */
  136. //$1 'Zend/Log/Exception.php';
  137. throw new Zend_Log_Exception("{$filterName} does not implement Zend_Log_Filter_Interface");
  138. }
  139. return $filter;
  140. }
  141. /**
  142. * Construct a filter or writer from config
  143. *
  144. * @param string $type 'writer' of 'filter'
  145. * @param mixed $config Zend_Config or Array
  146. * @param string $namespace
  147. * @return object
  148. */
  149. protected function _constructFromConfig($type, $config, $namespace)
  150. {
  151. if ($config instanceof Zend_Config) {
  152. $config = $config->toArray();
  153. }
  154. if (!is_array($config) || empty($config)) {
  155. //$1 'Zend/Log/Exception.php';
  156. throw new Zend_Log_Exception(
  157. 'Configuration must be an array or instance of Zend_Config'
  158. );
  159. }
  160. $params = isset($config[ $type .'Params' ]) ? $config[ $type .'Params' ] : array();
  161. $className = $this->getClassName($config, $type, $namespace);
  162. if (!class_exists($className)) {
  163. //$1 'Zend/Loader.php';
  164. Zend_Loader::loadClass($className);
  165. }
  166. $reflection = new ReflectionClass($className);
  167. if (!$reflection->implementsInterface('Zend_Log_FactoryInterface')) {
  168. //$1 'Zend/Log/Exception.php';
  169. throw new Zend_Log_Exception(
  170. 'Driver does not implement Zend_Log_FactoryInterface and can not be constructed from config.'
  171. );
  172. }
  173. return call_user_func(array($className, 'factory'), $params);
  174. }
  175. /**
  176. * Get the writer or filter full classname
  177. *
  178. * @param array $config
  179. * @param string $type filter|writer
  180. * @param string $defaultNamespace
  181. * @return string full classname
  182. */
  183. protected function getClassName($config, $type, $defaultNamespace)
  184. {
  185. if (!isset($config[ $type . 'Name' ])) {
  186. //$1 'Zend/Log/Exception.php';
  187. throw new Zend_Log_Exception("Specify {$type}Name in the configuration array");
  188. }
  189. $className = $config[ $type . 'Name' ];
  190. $namespace = $defaultNamespace;
  191. if (isset($config[ $type . 'Namespace' ])) {
  192. $namespace = $config[ $type . 'Namespace' ];
  193. }
  194. $fullClassName = $namespace . '_' . $className;
  195. return $fullClassName;
  196. }
  197. /**
  198. * Class destructor. Shutdown log writers
  199. *
  200. * @return void
  201. */
  202. public function __destruct()
  203. {
  204. foreach($this->_writers as $writer) {
  205. $writer->shutdown();
  206. }
  207. }
  208. /**
  209. * Undefined method handler allows a shortcut:
  210. * $log->priorityName('message')
  211. * instead of
  212. * $log->log('message', Zend_Log::PRIORITY_NAME)
  213. *
  214. * @param string $method priority name
  215. * @param string $params message to log
  216. * @return void
  217. * @throws Zend_Log_Exception
  218. */
  219. public function __call($method, $params)
  220. {
  221. $priority = strtoupper($method);
  222. if (($priority = array_search($priority, $this->_priorities)) !== false) {
  223. switch (count($params)) {
  224. case 0:
  225. /** @see Zend_Log_Exception */
  226. //$1 'Zend/Log/Exception.php';
  227. throw new Zend_Log_Exception('Missing log message');
  228. case 1:
  229. $message = array_shift($params);
  230. $extras = null;
  231. break;
  232. default:
  233. $message = array_shift($params);
  234. $extras = array_shift($params);
  235. break;
  236. }
  237. $this->log($message, $priority, $extras);
  238. } else {
  239. /** @see Zend_Log_Exception */
  240. //$1 'Zend/Log/Exception.php';
  241. throw new Zend_Log_Exception('Bad log priority');
  242. }
  243. }
  244. /**
  245. * Log a message at a priority
  246. *
  247. * @param string $message Message to log
  248. * @param integer $priority Priority of message
  249. * @param mixed $extras Extra information to log in event
  250. * @return void
  251. * @throws Zend_Log_Exception
  252. */
  253. public function log($message, $priority, $extras = null)
  254. {
  255. // sanity checks
  256. if (empty($this->_writers)) {
  257. /** @see Zend_Log_Exception */
  258. //$1 'Zend/Log/Exception.php';
  259. throw new Zend_Log_Exception('No writers were added');
  260. }
  261. if (! isset($this->_priorities[$priority])) {
  262. /** @see Zend_Log_Exception */
  263. //$1 'Zend/Log/Exception.php';
  264. throw new Zend_Log_Exception('Bad log priority');
  265. }
  266. // pack into event required by filters and writers
  267. $event = array_merge(array('timestamp' => date('c'),
  268. 'message' => $message,
  269. 'priority' => $priority,
  270. 'priorityName' => $this->_priorities[$priority]),
  271. $this->_extras);
  272. // Check to see if any extra information was passed
  273. if (!empty($extras)) {
  274. $info = array();
  275. if (is_array($extras)) {
  276. foreach ($extras as $key => $value) {
  277. if (is_string($key)) {
  278. $event[$key] = $value;
  279. } else {
  280. $info[] = $value;
  281. }
  282. }
  283. } else {
  284. $info = $extras;
  285. }
  286. if (!empty($info)) {
  287. $event['info'] = $info;
  288. }
  289. }
  290. // abort if rejected by the global filters
  291. foreach ($this->_filters as $filter) {
  292. if (! $filter->accept($event)) {
  293. return;
  294. }
  295. }
  296. // send to each writer
  297. foreach ($this->_writers as $writer) {
  298. $writer->write($event);
  299. }
  300. }
  301. /**
  302. * Add a custom priority
  303. *
  304. * @param string $name Name of priority
  305. * @param integer $priority Numeric priority
  306. * @throws Zend_Log_InvalidArgumentException
  307. */
  308. public function addPriority($name, $priority)
  309. {
  310. // Priority names must be uppercase for predictability.
  311. $name = strtoupper($name);
  312. if (isset($this->_priorities[$priority])
  313. || array_search($name, $this->_priorities)) {
  314. /** @see Zend_Log_Exception */
  315. //$1 'Zend/Log/Exception.php';
  316. throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
  317. }
  318. $this->_priorities[$priority] = $name;
  319. }
  320. /**
  321. * Add a filter that will be applied before all log writers.
  322. * Before a message will be received by any of the writers, it
  323. * must be accepted by all filters added with this method.
  324. *
  325. * @param int|Zend_Log_Filter_Interface $filter
  326. * @return void
  327. */
  328. public function addFilter($filter)
  329. {
  330. if (is_integer($filter)) {
  331. /** @see Zend_Log_Filter_Priority */
  332. //$1 'Zend/Log/Filter/Priority.php';
  333. $filter = new Zend_Log_Filter_Priority($filter);
  334. } elseif ($filter instanceof Zend_Config || is_array($filter)) {
  335. $filter = $this->_constructFilterFromConfig($filter);
  336. } elseif(! $filter instanceof Zend_Log_Filter_Interface) {
  337. /** @see Zend_Log_Exception */
  338. //$1 'Zend/Log/Exception.php';
  339. throw new Zend_Log_Exception('Invalid filter provided');
  340. }
  341. $this->_filters[] = $filter;
  342. }
  343. /**
  344. * Add a writer. A writer is responsible for taking a log
  345. * message and writing it out to storage.
  346. *
  347. * @param mixed $writer Zend_Log_Writer_Abstract or Config array
  348. * @return void
  349. */
  350. public function addWriter($writer)
  351. {
  352. if (is_array($writer) || $writer instanceof Zend_Config) {
  353. $writer = $this->_constructWriterFromConfig($writer);
  354. }
  355. if (!$writer instanceof Zend_Log_Writer_Abstract) {
  356. /** @see Zend_Log_Exception */
  357. //$1 'Zend/Log/Exception.php';
  358. throw new Zend_Log_Exception(
  359. 'Writer must be an instance of Zend_Log_Writer_Abstract'
  360. . ' or you should pass a configuration array'
  361. );
  362. }
  363. $this->_writers[] = $writer;
  364. }
  365. /**
  366. * Set an extra item to pass to the log writers.
  367. *
  368. * @param $name Name of the field
  369. * @param $value Value of the field
  370. * @return void
  371. */
  372. public function setEventItem($name, $value) {
  373. $this->_extras = array_merge($this->_extras, array($name => $value));
  374. }
  375. }