Log.php 13 KB

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