PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Microsoft/Log.php

https://bitbucket.org/ktos/tinyshare
PHP | 426 lines | 217 code | 49 blank | 160 comment | 32 complexity | b0b5ee1879bffe57447b9345e4ae3596 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 Microsoft
  16. * @package Microsoft_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: Log.php 22632 2010-07-18 18:30:08Z ramon $
  20. */
  21. /**
  22. * @see Microsoft_AutoLoader
  23. */
  24. require_once dirname(__FILE__) . '/AutoLoader.php';
  25. /**
  26. * @category Microsoft
  27. * @package Microsoft_Log
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id: Log.php 22632 2010-07-18 18:30:08Z ramon $
  31. */
  32. class Microsoft_Log
  33. {
  34. const EMERG = 0; // Emergency: system is unusable
  35. const ALERT = 1; // Alert: action must be taken immediately
  36. const CRIT = 2; // Critical: critical conditions
  37. const ERR = 3; // Error: error conditions
  38. const WARN = 4; // Warning: warning conditions
  39. const NOTICE = 5; // Notice: normal but significant condition
  40. const INFO = 6; // Informational: informational messages
  41. const DEBUG = 7; // Debug: debug messages
  42. /**
  43. * @var array of priorities where the keys are the
  44. * priority numbers and the values are the priority names
  45. */
  46. protected $_priorities = array();
  47. /**
  48. * @var array of Microsoft_Log_Writer_Abstract
  49. */
  50. protected $_writers = array();
  51. /**
  52. * @var array of Microsoft_Log_Filter_Interface
  53. */
  54. protected $_filters = array();
  55. /**
  56. * @var array of extra log event
  57. */
  58. protected $_extras = array();
  59. /**
  60. *
  61. * @var string
  62. */
  63. protected $_defaultWriterNamespace = 'Microsoft_Log_Writer';
  64. /**
  65. *
  66. * @var string
  67. */
  68. protected $_defaultFilterNamespace = 'Microsoft_Log_Filter';
  69. /**
  70. * Class constructor. Create a new logger
  71. *
  72. * @param Microsoft_Log_Writer_Abstract|null $writer default writer
  73. */
  74. public function __construct(Microsoft_Log_Writer_Abstract $writer = null)
  75. {
  76. $r = new ReflectionClass($this);
  77. $this->_priorities = array_flip($r->getConstants());
  78. if ($writer !== null) {
  79. $this->addWriter($writer);
  80. }
  81. }
  82. /**
  83. * Factory to construct the logger and one or more writers
  84. * based on the configuration array
  85. *
  86. * @param array $config
  87. * @return Microsoft_Log
  88. */
  89. static public function factory($config = array())
  90. {
  91. if (!is_array($config) || empty($config)) {
  92. /** @see Microsoft_Log_Exception */
  93. require_once 'Microsoft/Log/Exception.php';
  94. throw new Microsoft_Log_Exception('Configuration must be an array');
  95. }
  96. $log = new Microsoft_Log;
  97. if (!is_array(current($config))) {
  98. $log->addWriter(current($config));
  99. } else {
  100. foreach($config as $writer) {
  101. $log->addWriter($writer);
  102. }
  103. }
  104. return $log;
  105. }
  106. /**
  107. * Construct a writer object based on a configuration array
  108. *
  109. * @param array $spec config array with writer spec
  110. * @return Microsoft_Log_Writer_Abstract
  111. */
  112. protected function _constructWriterFromConfig($config)
  113. {
  114. $writer = $this->_constructFromConfig('writer', $config, $this->_defaultWriterNamespace);
  115. if (!$writer instanceof Microsoft_Log_Writer_Abstract) {
  116. $writerName = is_object($writer)
  117. ? get_class($writer)
  118. : 'The specified writer';
  119. /** @see Microsoft_Log_Exception */
  120. require_once 'Microsoft/Log/Exception.php';
  121. throw new Microsoft_Log_Exception("{$writerName} does not extend Microsoft_Log_Writer_Abstract!");
  122. }
  123. if (isset($config['filterName'])) {
  124. $filter = $this->_constructFilterFromConfig($config);
  125. $writer->addFilter($filter);
  126. }
  127. return $writer;
  128. }
  129. /**
  130. * Construct filter object from configuration array or Microsoft_Config object
  131. *
  132. * @param array $config
  133. * @return Microsoft_Log_Filter_Interface
  134. */
  135. protected function _constructFilterFromConfig($config)
  136. {
  137. $filter = $this->_constructFromConfig('filter', $config, $this->_defaultFilterNamespace);
  138. if (!$filter instanceof Microsoft_Log_Filter_Interface) {
  139. $filterName = is_object($filter)
  140. ? get_class($filter)
  141. : 'The specified filter';
  142. /** @see Microsoft_Log_Exception */
  143. require_once 'Microsoft/Log/Exception.php';
  144. throw new Microsoft_Log_Exception("{$filterName} does not implement Microsoft_Log_Filter_Interface");
  145. }
  146. return $filter;
  147. }
  148. /**
  149. * Construct a filter or writer from config
  150. *
  151. * @param string $type 'writer' of 'filter'
  152. * @param array $config
  153. * @param string $namespace
  154. * @return object
  155. */
  156. protected function _constructFromConfig($type, $config, $namespace)
  157. {
  158. if (!is_array($config) || empty($config)) {
  159. require_once 'Microsoft/Log/Exception.php';
  160. throw new Microsoft_Log_Exception('Configuration must be an array');
  161. }
  162. $params = isset($config[ $type .'Params' ]) ? $config[ $type .'Params' ] : array();
  163. $className = $this->getClassName($config, $type, $namespace);
  164. if (!class_exists($className)) {
  165. require_once 'Microsoft/Loader.php';
  166. Microsoft_Loader::loadClass($className);
  167. }
  168. $reflection = new ReflectionClass($className);
  169. if (!$reflection->implementsInterface('Microsoft_Log_FactoryInterface')) {
  170. require_once 'Zend/Log/Exception.php';
  171. throw new Microsoft_Log_Exception(
  172. 'Driver does not implement Microsoft_Log_FactoryInterface and can not be constructed from config.'
  173. );
  174. }
  175. return call_user_func(array($className, 'factory'), $params);
  176. }
  177. /**
  178. * Get the writer or filter full classname
  179. *
  180. * @param array $config
  181. * @param string $type filter|writer
  182. * @param string $defaultNamespace
  183. * @return string full classname
  184. */
  185. protected function getClassName($config, $type, $defaultNamespace)
  186. {
  187. if (!isset($config[ $type . 'Name' ])) {
  188. require_once 'Zend/Log/Exception.php';
  189. throw new Microsoft_Log_Exception("Specify {$type}Name in the configuration array");
  190. }
  191. $className = $config[ $type . 'Name' ];
  192. $namespace = $defaultNamespace;
  193. if (isset($config[ $type . 'Namespace' ])) {
  194. $namespace = $config[ $type . 'Namespace' ];
  195. }
  196. $fullClassName = $namespace . '_' . $className;
  197. return $fullClassName;
  198. }
  199. /**
  200. * Class destructor. Shutdown log writers
  201. *
  202. * @return void
  203. */
  204. public function __destruct()
  205. {
  206. foreach($this->_writers as $writer) {
  207. $writer->shutdown();
  208. }
  209. }
  210. /**
  211. * Undefined method handler allows a shortcut:
  212. * $log->priorityName('message')
  213. * instead of
  214. * $log->log('message', Microsoft_Log::PRIORITY_NAME)
  215. *
  216. * @param string $method priority name
  217. * @param string $params message to log
  218. * @return void
  219. * @throws Microsoft_Log_Exception
  220. */
  221. public function __call($method, $params)
  222. {
  223. $priority = strtoupper($method);
  224. if (($priority = array_search($priority, $this->_priorities)) !== false) {
  225. switch (count($params)) {
  226. case 0:
  227. /** @see Microsoft_Log_Exception */
  228. require_once 'Microsoft_/Log/Exception.php';
  229. throw new Microsoft_Log_Exception('Missing log message');
  230. case 1:
  231. $message = array_shift($params);
  232. $extras = null;
  233. break;
  234. default:
  235. $message = array_shift($params);
  236. $extras = array_shift($params);
  237. break;
  238. }
  239. $this->log($message, $priority, $extras);
  240. } else {
  241. /** @see Microsoft_Log_Exception */
  242. require_once 'Microsoft/Log/Exception.php';
  243. throw new Microsoft_Log_Exception('Bad log priority');
  244. }
  245. }
  246. /**
  247. * Log a message at a priority
  248. *
  249. * @param string $message Message to log
  250. * @param integer $priority Priority of message
  251. * @param mixed $extras Extra information to log in event
  252. * @return void
  253. * @throws Microsoft_Log_Exception
  254. */
  255. public function log($message, $priority, $extras = null)
  256. {
  257. // sanity checks
  258. if (empty($this->_writers)) {
  259. /** @see Microsoft_Log_Exception */
  260. require_once 'Microsoft/Log/Exception.php';
  261. throw new Microsoft_Log_Exception('No writers were added');
  262. }
  263. if (! isset($this->_priorities[$priority])) {
  264. /** @see Microsoft_Log_Exception */
  265. require_once 'Microsoft/Log/Exception.php';
  266. throw new Microsoft_Log_Exception('Bad log priority');
  267. }
  268. // pack into event required by filters and writers
  269. $event = array_merge(array('timestamp' => date('c'),
  270. 'message' => $message,
  271. 'priority' => $priority,
  272. 'priorityName' => $this->_priorities[$priority]),
  273. $this->_extras);
  274. // Check to see if any extra information was passed
  275. if (!empty($extras)) {
  276. $info = array();
  277. if (is_array($extras)) {
  278. foreach ($extras as $key => $value) {
  279. if (is_string($key)) {
  280. $event[$key] = $value;
  281. } else {
  282. $info[] = $value;
  283. }
  284. }
  285. } else {
  286. $info = $extras;
  287. }
  288. if (!empty($info)) {
  289. $event['info'] = $info;
  290. }
  291. }
  292. // abort if rejected by the global filters
  293. foreach ($this->_filters as $filter) {
  294. if (! $filter->accept($event)) {
  295. return;
  296. }
  297. }
  298. // send to each writer
  299. foreach ($this->_writers as $writer) {
  300. $writer->write($event);
  301. }
  302. }
  303. /**
  304. * Add a custom priority
  305. *
  306. * @param string $name Name of priority
  307. * @param integer $priority Numeric priority
  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. || false !== array_search($name, $this->_priorities)) {
  315. /** @see Microsoft_Log_Exception */
  316. require_once 'Microsoft/Log/Exception.php';
  317. throw new Microsoft_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|Microsoft_Log_Filter_Interface $filter
  327. * @return void
  328. */
  329. public function addFilter($filter)
  330. {
  331. if (is_integer($filter)) {
  332. /** @see Microsoft_Log_Filter_Priority */
  333. require_once 'Microsoft/Log/Filter/Priority.php';
  334. $filter = new Microsoft_Log_Filter_Priority($filter);
  335. } elseif ($filter instanceof Microsoft_Config || is_array($filter)) {
  336. $filter = $this->_constructFilterFromConfig($filter);
  337. } elseif(! $filter instanceof Microsoft_Log_Filter_Interface) {
  338. /** @see Microsoft_Log_Exception */
  339. require_once 'Microsoft/Log/Exception.php';
  340. throw new Microsoft_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 Microsoft_Log_Writer_Abstract or Config array
  349. * @return void
  350. */
  351. public function addWriter($writer)
  352. {
  353. if (is_array($writer)) {
  354. $writer = $this->_constructWriterFromConfig($writer);
  355. }
  356. if (!$writer instanceof Microsoft_Log_Writer_Abstract) {
  357. /** @see Microsoft_Log_Exception */
  358. require_once 'Microsoft/Log/Exception.php';
  359. throw new Microsoft_Log_Exception(
  360. 'Writer must be an instance of Microsoft_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. {
  375. $this->_extras = array_merge($this->_extras, array($name => $value));
  376. }
  377. }