PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Log.php

https://github.com/damoon/zf
PHP | 440 lines | 224 code | 51 blank | 165 comment | 35 complexity | 2e5e916b35fd8d425fe075bb67a8de9b 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$
  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. 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. /** @see Zend_Log_Exception */
  116. require_once '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. require_once '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. require_once '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. require_once 'Zend/Loader.php';
  164. Zend_Loader::loadClass($className);
  165. }
  166. $reflection = new ReflectionClass($className);
  167. if (!$reflection->implementsInterface('Zend_Log_FactoryInterface')) {
  168. require_once '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. require_once '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. * Packs message and priority into Event array
  199. *
  200. * @param string $message Message to log
  201. * @param integer $priority Priority of message
  202. * @return array Event array
  203. **/
  204. protected function _packEvent($message, $priority)
  205. {
  206. return array_merge(array(
  207. 'timestamp' => date('c'),
  208. 'message' => $message,
  209. 'priority' => $priority,
  210. 'priorityName' => $this->_priorities[$priority]
  211. ),
  212. $this->_extras
  213. );
  214. }
  215. /**
  216. * Class destructor. Shutdown log writers
  217. *
  218. * @return void
  219. */
  220. public function __destruct()
  221. {
  222. foreach($this->_writers as $writer) {
  223. $writer->shutdown();
  224. }
  225. }
  226. /**
  227. * Undefined method handler allows a shortcut:
  228. * $log->priorityName('message')
  229. * instead of
  230. * $log->log('message', Zend_Log::PRIORITY_NAME)
  231. *
  232. * @param string $method priority name
  233. * @param string $params message to log
  234. * @return void
  235. * @throws Zend_Log_Exception
  236. */
  237. public function __call($method, $params)
  238. {
  239. $priority = strtoupper($method);
  240. if (($priority = array_search($priority, $this->_priorities)) !== false) {
  241. switch (count($params)) {
  242. case 0:
  243. /** @see Zend_Log_Exception */
  244. require_once 'Zend/Log/Exception.php';
  245. throw new Zend_Log_Exception('Missing log message');
  246. case 1:
  247. $message = array_shift($params);
  248. $extras = null;
  249. break;
  250. default:
  251. $message = array_shift($params);
  252. $extras = array_shift($params);
  253. break;
  254. }
  255. $this->log($message, $priority, $extras);
  256. } else {
  257. /** @see Zend_Log_Exception */
  258. require_once 'Zend/Log/Exception.php';
  259. throw new Zend_Log_Exception('Bad log priority');
  260. }
  261. }
  262. /**
  263. * Log a message at a priority
  264. *
  265. * @param string $message Message to log
  266. * @param integer $priority Priority of message
  267. * @param mixed $extras Extra information to log in event
  268. * @return void
  269. * @throws Zend_Log_Exception
  270. */
  271. public function log($message, $priority, $extras = null)
  272. {
  273. // sanity checks
  274. if (empty($this->_writers)) {
  275. /** @see Zend_Log_Exception */
  276. require_once 'Zend/Log/Exception.php';
  277. throw new Zend_Log_Exception('No writers were added');
  278. }
  279. if (! isset($this->_priorities[$priority])) {
  280. /** @see Zend_Log_Exception */
  281. require_once 'Zend/Log/Exception.php';
  282. throw new Zend_Log_Exception('Bad log priority');
  283. }
  284. // pack into event required by filters and writers
  285. $event = $this->_packEvent($message, $priority);
  286. // Check to see if any extra information was passed
  287. if (!empty($extras)) {
  288. $info = array();
  289. if (is_array($extras)) {
  290. foreach ($extras as $key => $value) {
  291. if (is_string($key)) {
  292. $event[$key] = $value;
  293. } else {
  294. $info[] = $value;
  295. }
  296. }
  297. } else {
  298. $info = $extras;
  299. }
  300. if (!empty($info)) {
  301. $event['info'] = $info;
  302. }
  303. }
  304. // abort if rejected by the global filters
  305. foreach ($this->_filters as $filter) {
  306. if (! $filter->accept($event)) {
  307. return;
  308. }
  309. }
  310. // send to each writer
  311. foreach ($this->_writers as $writer) {
  312. $writer->write($event);
  313. }
  314. }
  315. /**
  316. * Add a custom priority
  317. *
  318. * @param string $name Name of priority
  319. * @param integer $priority Numeric priority
  320. * @throws Zend_Log_InvalidArgumentException
  321. */
  322. public function addPriority($name, $priority)
  323. {
  324. // Priority names must be uppercase for predictability.
  325. $name = strtoupper($name);
  326. if (isset($this->_priorities[$priority])
  327. || array_search($name, $this->_priorities)) {
  328. /** @see Zend_Log_Exception */
  329. require_once 'Zend/Log/Exception.php';
  330. throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
  331. }
  332. $this->_priorities[$priority] = $name;
  333. }
  334. /**
  335. * Add a filter that will be applied before all log writers.
  336. * Before a message will be received by any of the writers, it
  337. * must be accepted by all filters added with this method.
  338. *
  339. * @param int|Zend_Log_Filter_Interface $filter
  340. * @return void
  341. */
  342. public function addFilter($filter)
  343. {
  344. if (is_integer($filter)) {
  345. /** @see Zend_Log_Filter_Priority */
  346. require_once 'Zend/Log/Filter/Priority.php';
  347. $filter = new Zend_Log_Filter_Priority($filter);
  348. } elseif ($filter instanceof Zend_Config || is_array($filter)) {
  349. $filter = $this->_constructFilterFromConfig($filter);
  350. } elseif(! $filter instanceof Zend_Log_Filter_Interface) {
  351. /** @see Zend_Log_Exception */
  352. require_once 'Zend/Log/Exception.php';
  353. throw new Zend_Log_Exception('Invalid filter provided');
  354. }
  355. $this->_filters[] = $filter;
  356. }
  357. /**
  358. * Add a writer. A writer is responsible for taking a log
  359. * message and writing it out to storage.
  360. *
  361. * @param mixed $writer Zend_Log_Writer_Abstract or Config array
  362. * @return void
  363. */
  364. public function addWriter($writer)
  365. {
  366. if (is_array($writer) || $writer instanceof Zend_Config) {
  367. $writer = $this->_constructWriterFromConfig($writer);
  368. }
  369. if (!$writer instanceof Zend_Log_Writer_Abstract) {
  370. /** @see Zend_Log_Exception */
  371. require_once 'Zend/Log/Exception.php';
  372. throw new Zend_Log_Exception(
  373. 'Writer must be an instance of Zend_Log_Writer_Abstract'
  374. . ' or you should pass a configuration array'
  375. );
  376. }
  377. $this->_writers[] = $writer;
  378. }
  379. /**
  380. * Set an extra item to pass to the log writers.
  381. *
  382. * @param $name Name of the field
  383. * @param $value Value of the field
  384. * @return void
  385. */
  386. public function setEventItem($name, $value) {
  387. $this->_extras = array_merge($this->_extras, array($name => $value));
  388. }
  389. }