Log.php 13 KB

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