Log.php 13 KB

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