Base for a static organization website

CakeLog.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. /**
  3. * Logging.
  4. *
  5. * Log messages to text files.
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Log
  17. * @since CakePHP(tm) v 0.2.9
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('LogEngineCollection', 'Log');
  21. /**
  22. * Logs messages to configured Log adapters. One or more adapters
  23. * can be configured using CakeLogs's methods. If you don't
  24. * configure any adapters, and write to the logs a default
  25. * FileLog will be autoconfigured for you.
  26. *
  27. * ### Configuring Log adapters
  28. *
  29. * You can configure log adapters in your applications `bootstrap.php` file.
  30. * A sample configuration would look like:
  31. *
  32. * ```
  33. * CakeLog::config('my_log', array('engine' => 'File'));
  34. * ```
  35. *
  36. * See the documentation on CakeLog::config() for more detail.
  37. *
  38. * ### Writing to the log
  39. *
  40. * You write to the logs using CakeLog::write(). See its documentation for more
  41. * information.
  42. *
  43. * ### Logging Levels
  44. *
  45. * By default CakeLog supports all the log levels defined in
  46. * RFC 5424. When logging messages you can either use the named methods,
  47. * or the correct constants with `write()`:
  48. *
  49. * ```
  50. * CakeLog::error('Something horrible happened');
  51. * CakeLog::write(LOG_ERR, 'Something horrible happened');
  52. * ```
  53. *
  54. * If you require custom logging levels, you can use CakeLog::levels() to
  55. * append additional logging levels.
  56. *
  57. * ### Logging scopes
  58. *
  59. * When logging messages and configuring log adapters, you can specify
  60. * 'scopes' that the logger will handle. You can think of scopes as subsystems
  61. * in your application that may require different logging setups. For
  62. * example in an e-commerce application you may want to handle logged errors
  63. * in the cart and ordering subsystems differently than the rest of the
  64. * application. By using scopes you can control logging for each part
  65. * of your application and still keep standard log levels.
  66. *
  67. * See CakeLog::config() and CakeLog::write() for more information
  68. * on scopes
  69. *
  70. * @package Cake.Log
  71. * @link http://book.cakephp.org/2.0/en/core-libraries/logging.html#logging
  72. */
  73. class CakeLog {
  74. /**
  75. * LogEngineCollection class
  76. *
  77. * @var LogEngineCollection
  78. */
  79. protected static $_Collection;
  80. /**
  81. * Default log levels as detailed in RFC 5424
  82. * http://tools.ietf.org/html/rfc5424
  83. *
  84. * @var array
  85. */
  86. protected static $_defaultLevels = array(
  87. 'emergency' => LOG_EMERG,
  88. 'alert' => LOG_ALERT,
  89. 'critical' => LOG_CRIT,
  90. 'error' => LOG_ERR,
  91. 'warning' => LOG_WARNING,
  92. 'notice' => LOG_NOTICE,
  93. 'info' => LOG_INFO,
  94. 'debug' => LOG_DEBUG,
  95. );
  96. /**
  97. * Active log levels for this instance.
  98. *
  99. * @var array
  100. */
  101. protected static $_levels;
  102. /**
  103. * Mapped log levels
  104. *
  105. * @var array
  106. */
  107. protected static $_levelMap;
  108. /**
  109. * initialize ObjectCollection
  110. *
  111. * @return void
  112. */
  113. protected static function _init() {
  114. static::$_levels = static::defaultLevels();
  115. static::$_Collection = new LogEngineCollection();
  116. }
  117. /**
  118. * Configure and add a new logging stream to CakeLog
  119. * You can use add loggers from app/Log/Engine use app.loggername, or any
  120. * plugin/Log/Engine using plugin.loggername.
  121. *
  122. * ### Usage:
  123. *
  124. * ```
  125. * CakeLog::config('second_file', array(
  126. * 'engine' => 'File',
  127. * 'path' => '/var/logs/my_app/'
  128. * ));
  129. * ```
  130. *
  131. * Will configure a FileLog instance to use the specified path.
  132. * All options that are not `engine` are passed onto the logging adapter,
  133. * and handled there. Any class can be configured as a logging
  134. * adapter as long as it implements the methods in CakeLogInterface.
  135. *
  136. * ### Logging levels
  137. *
  138. * When configuring loggers, you can set which levels a logger will handle.
  139. * This allows you to disable debug messages in production for example:
  140. *
  141. * ```
  142. * CakeLog::config('default', array(
  143. * 'engine' => 'File',
  144. * 'path' => LOGS,
  145. * 'levels' => array('error', 'critical', 'alert', 'emergency')
  146. * ));
  147. * ```
  148. *
  149. * The above logger would only log error messages or higher. Any
  150. * other log messages would be discarded.
  151. *
  152. * ### Logging scopes
  153. *
  154. * When configuring loggers you can define the active scopes the logger
  155. * is for. If defined only the listed scopes will be handled by the
  156. * logger. If you don't define any scopes an adapter will catch
  157. * all scopes that match the handled levels.
  158. *
  159. * ```
  160. * CakeLog::config('payments', array(
  161. * 'engine' => 'File',
  162. * 'types' => array('info', 'error', 'warning'),
  163. * 'scopes' => array('payment', 'order')
  164. * ));
  165. * ```
  166. *
  167. * The above logger will only capture log entries made in the
  168. * `payment` and `order` scopes. All other scopes including the
  169. * undefined scope will be ignored. Its important to remember that
  170. * when using scopes you must also define the `types` of log messages
  171. * that a logger will handle. Failing to do so will result in the logger
  172. * catching all log messages even if the scope is incorrect.
  173. *
  174. * @param string $key The keyname for this logger, used to remove the
  175. * logger later.
  176. * @param array $config Array of configuration information for the logger
  177. * @return bool success of configuration.
  178. * @throws CakeLogException
  179. * @link http://book.cakephp.org/2.0/en/core-libraries/logging.html#creating-and-configuring-log-streams
  180. */
  181. public static function config($key, $config) {
  182. if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $key)) {
  183. throw new CakeLogException(__d('cake_dev', 'Invalid key name'));
  184. }
  185. if (empty($config['engine'])) {
  186. throw new CakeLogException(__d('cake_dev', 'Missing logger class name'));
  187. }
  188. if (empty(static::$_Collection)) {
  189. static::_init();
  190. }
  191. static::$_Collection->load($key, $config);
  192. return true;
  193. }
  194. /**
  195. * Returns the keynames of the currently active streams
  196. *
  197. * @return array Array of configured log streams.
  198. */
  199. public static function configured() {
  200. if (empty(static::$_Collection)) {
  201. static::_init();
  202. }
  203. return static::$_Collection->loaded();
  204. }
  205. /**
  206. * Gets/sets log levels
  207. *
  208. * Call this method without arguments, eg: `CakeLog::levels()` to obtain current
  209. * level configuration.
  210. *
  211. * To append additional level 'user0' and 'user1' to to default log levels:
  212. *
  213. * ```
  214. * CakeLog::levels(array('user0, 'user1'));
  215. * // or
  216. * CakeLog::levels(array('user0, 'user1'), true);
  217. * ```
  218. *
  219. * will result in:
  220. *
  221. * ```
  222. * array(
  223. * 0 => 'emergency',
  224. * 1 => 'alert',
  225. * ...
  226. * 8 => 'user0',
  227. * 9 => 'user1',
  228. * );
  229. * ```
  230. *
  231. * To set/replace existing configuration, pass an array with the second argument
  232. * set to false.
  233. *
  234. * ```
  235. * CakeLog::levels(array('user0, 'user1'), false);
  236. * ```
  237. *
  238. * will result in:
  239. *
  240. * ```
  241. * array(
  242. * 0 => 'user0',
  243. * 1 => 'user1',
  244. * );
  245. * ```
  246. *
  247. * @param array $levels array
  248. * @param bool $append true to append, false to replace
  249. * @return array Active log levels
  250. */
  251. public static function levels($levels = array(), $append = true) {
  252. if (empty(static::$_Collection)) {
  253. static::_init();
  254. }
  255. if (empty($levels)) {
  256. return static::$_levels;
  257. }
  258. $levels = array_values($levels);
  259. if ($append) {
  260. static::$_levels = array_merge(static::$_levels, $levels);
  261. } else {
  262. static::$_levels = $levels;
  263. }
  264. static::$_levelMap = array_flip(static::$_levels);
  265. return static::$_levels;
  266. }
  267. /**
  268. * Reset log levels to the original value
  269. *
  270. * @return array Default log levels
  271. */
  272. public static function defaultLevels() {
  273. static::$_levelMap = static::$_defaultLevels;
  274. static::$_levels = array_flip(static::$_levelMap);
  275. return static::$_levels;
  276. }
  277. /**
  278. * Removes a stream from the active streams. Once a stream has been removed
  279. * it will no longer have messages sent to it.
  280. *
  281. * @param string $streamName Key name of a configured stream to remove.
  282. * @return void
  283. */
  284. public static function drop($streamName) {
  285. if (empty(static::$_Collection)) {
  286. static::_init();
  287. }
  288. static::$_Collection->unload($streamName);
  289. }
  290. /**
  291. * Checks whether $streamName is enabled
  292. *
  293. * @param string $streamName to check
  294. * @return bool
  295. * @throws CakeLogException
  296. */
  297. public static function enabled($streamName) {
  298. if (empty(static::$_Collection)) {
  299. static::_init();
  300. }
  301. if (!isset(static::$_Collection->{$streamName})) {
  302. throw new CakeLogException(__d('cake_dev', 'Stream %s not found', $streamName));
  303. }
  304. return static::$_Collection->enabled($streamName);
  305. }
  306. /**
  307. * Enable stream. Streams that were previously disabled
  308. * can be re-enabled with this method.
  309. *
  310. * @param string $streamName to enable
  311. * @return void
  312. * @throws CakeLogException
  313. */
  314. public static function enable($streamName) {
  315. if (empty(static::$_Collection)) {
  316. static::_init();
  317. }
  318. if (!isset(static::$_Collection->{$streamName})) {
  319. throw new CakeLogException(__d('cake_dev', 'Stream %s not found', $streamName));
  320. }
  321. static::$_Collection->enable($streamName);
  322. }
  323. /**
  324. * Disable stream. Disabling a stream will
  325. * prevent that log stream from receiving any messages until
  326. * its re-enabled.
  327. *
  328. * @param string $streamName to disable
  329. * @return void
  330. * @throws CakeLogException
  331. */
  332. public static function disable($streamName) {
  333. if (empty(static::$_Collection)) {
  334. static::_init();
  335. }
  336. if (!isset(static::$_Collection->{$streamName})) {
  337. throw new CakeLogException(__d('cake_dev', 'Stream %s not found', $streamName));
  338. }
  339. static::$_Collection->disable($streamName);
  340. }
  341. /**
  342. * Gets the logging engine from the active streams.
  343. *
  344. * @param string $streamName Key name of a configured stream to get.
  345. * @return mixed instance of BaseLog or false if not found
  346. * @see BaseLog
  347. */
  348. public static function stream($streamName) {
  349. if (empty(static::$_Collection)) {
  350. static::_init();
  351. }
  352. if (!empty(static::$_Collection->{$streamName})) {
  353. return static::$_Collection->{$streamName};
  354. }
  355. return false;
  356. }
  357. /**
  358. * Writes the given message and type to all of the configured log adapters.
  359. * Configured adapters are passed both the $type and $message variables. $type
  360. * is one of the following strings/values.
  361. *
  362. * ### Types:
  363. *
  364. * - LOG_EMERG => 'emergency',
  365. * - LOG_ALERT => 'alert',
  366. * - LOG_CRIT => 'critical',
  367. * - `LOG_ERR` => 'error',
  368. * - `LOG_WARNING` => 'warning',
  369. * - `LOG_NOTICE` => 'notice',
  370. * - `LOG_INFO` => 'info',
  371. * - `LOG_DEBUG` => 'debug',
  372. *
  373. * ### Usage:
  374. *
  375. * Write a message to the 'warning' log:
  376. *
  377. * `CakeLog::write('warning', 'Stuff is broken here');`
  378. *
  379. * @param int|string $type Type of message being written. When value is an integer
  380. * or a string matching the recognized levels, then it will
  381. * be treated log levels. Otherwise it's treated as scope.
  382. * @param string $message Message content to log
  383. * @param string|array $scope The scope(s) a log message is being created in.
  384. * See CakeLog::config() for more information on logging scopes.
  385. * @return bool Success
  386. * @link http://book.cakephp.org/2.0/en/core-libraries/logging.html#writing-to-logs
  387. */
  388. public static function write($type, $message, $scope = array()) {
  389. if (empty(static::$_Collection)) {
  390. static::_init();
  391. }
  392. if (is_int($type) && isset(static::$_levels[$type])) {
  393. $type = static::$_levels[$type];
  394. }
  395. if (is_string($type) && empty($scope) && !in_array($type, static::$_levels)) {
  396. $scope = $type;
  397. }
  398. $logged = false;
  399. foreach (static::$_Collection->enabled() as $streamName) {
  400. $logger = static::$_Collection->{$streamName};
  401. $types = $scopes = $config = array();
  402. if (method_exists($logger, 'config')) {
  403. $config = $logger->config();
  404. }
  405. if (isset($config['types'])) {
  406. $types = $config['types'];
  407. }
  408. if (isset($config['scopes'])) {
  409. $scopes = $config['scopes'];
  410. }
  411. $inScope = (count(array_intersect((array)$scope, $scopes)) > 0);
  412. $correctLevel = in_array($type, $types);
  413. if (
  414. // No config is a catch all (bc mode)
  415. (empty($types) && empty($scopes)) ||
  416. // BC layer for mixing scope & level
  417. (in_array($type, $scopes)) ||
  418. // no scopes, but has level
  419. (empty($scopes) && $correctLevel) ||
  420. // exact scope + level
  421. ($correctLevel && $inScope)
  422. ) {
  423. $logger->write($type, $message);
  424. $logged = true;
  425. }
  426. }
  427. return $logged;
  428. }
  429. /**
  430. * Convenience method to log emergency messages
  431. *
  432. * @param string $message log message
  433. * @param string|array $scope The scope(s) a log message is being created in.
  434. * See CakeLog::config() for more information on logging scopes.
  435. * @return bool Success
  436. */
  437. public static function emergency($message, $scope = array()) {
  438. return static::write(static::$_levelMap['emergency'], $message, $scope);
  439. }
  440. /**
  441. * Convenience method to log alert messages
  442. *
  443. * @param string $message log message
  444. * @param string|array $scope The scope(s) a log message is being created in.
  445. * See CakeLog::config() for more information on logging scopes.
  446. * @return bool Success
  447. */
  448. public static function alert($message, $scope = array()) {
  449. return static::write(static::$_levelMap['alert'], $message, $scope);
  450. }
  451. /**
  452. * Convenience method to log critical messages
  453. *
  454. * @param string $message log message
  455. * @param string|array $scope The scope(s) a log message is being created in.
  456. * See CakeLog::config() for more information on logging scopes.
  457. * @return bool Success
  458. */
  459. public static function critical($message, $scope = array()) {
  460. return static::write(static::$_levelMap['critical'], $message, $scope);
  461. }
  462. /**
  463. * Convenience method to log error messages
  464. *
  465. * @param string $message log message
  466. * @param string|array $scope The scope(s) a log message is being created in.
  467. * See CakeLog::config() for more information on logging scopes.
  468. * @return bool Success
  469. */
  470. public static function error($message, $scope = array()) {
  471. return static::write(static::$_levelMap['error'], $message, $scope);
  472. }
  473. /**
  474. * Convenience method to log warning messages
  475. *
  476. * @param string $message log message
  477. * @param string|array $scope The scope(s) a log message is being created in.
  478. * See CakeLog::config() for more information on logging scopes.
  479. * @return bool Success
  480. */
  481. public static function warning($message, $scope = array()) {
  482. return static::write(static::$_levelMap['warning'], $message, $scope);
  483. }
  484. /**
  485. * Convenience method to log notice messages
  486. *
  487. * @param string $message log message
  488. * @param string|array $scope The scope(s) a log message is being created in.
  489. * See CakeLog::config() for more information on logging scopes.
  490. * @return bool Success
  491. */
  492. public static function notice($message, $scope = array()) {
  493. return static::write(static::$_levelMap['notice'], $message, $scope);
  494. }
  495. /**
  496. * Convenience method to log debug messages
  497. *
  498. * @param string $message log message
  499. * @param string|array $scope The scope(s) a log message is being created in.
  500. * See CakeLog::config() for more information on logging scopes.
  501. * @return bool Success
  502. */
  503. public static function debug($message, $scope = array()) {
  504. return static::write(static::$_levelMap['debug'], $message, $scope);
  505. }
  506. /**
  507. * Convenience method to log info messages
  508. *
  509. * @param string $message log message
  510. * @param string|array $scope The scope(s) a log message is being created in.
  511. * See CakeLog::config() for more information on logging scopes.
  512. * @return bool Success
  513. */
  514. public static function info($message, $scope = array()) {
  515. return static::write(static::$_levelMap['info'], $message, $scope);
  516. }
  517. }