PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Log/CakeLog.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 208 lines | 76 code | 11 blank | 121 comment | 12 complexity | d0ce55f1a28e6360bdc6173aa33a6074 MD5 | raw file
  1. <?php
  2. /**
  3. * Logging.
  4. *
  5. * Log messages to text files.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Log
  18. * @since CakePHP(tm) v 0.2.9
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. /**
  22. * Set up error level constants to be used within the framework if they are not defined within the
  23. * system.
  24. *
  25. */
  26. if (!defined('LOG_WARNING')) {
  27. define('LOG_WARNING', 3);
  28. }
  29. if (!defined('LOG_NOTICE')) {
  30. define('LOG_NOTICE', 4);
  31. }
  32. if (!defined('LOG_DEBUG')) {
  33. define('LOG_DEBUG', 5);
  34. }
  35. if (!defined('LOG_INFO')) {
  36. define('LOG_INFO', 6);
  37. }
  38. /**
  39. * Logs messages to configured Log adapters. One or more adapters can be configured
  40. * using CakeLogs's methods. If you don't configure any adapters, and write to the logs
  41. * a default FileLog will be autoconfigured for you.
  42. *
  43. * ### Configuring Log adapters
  44. *
  45. * You can configure log adapters in your applications `bootstrap.php` file. A sample configuration
  46. * would look like:
  47. *
  48. * `CakeLog::config('my_log', array('engine' => 'FileLog'));`
  49. *
  50. * See the documentation on CakeLog::config() for more detail.
  51. *
  52. * ### Writing to the log
  53. *
  54. * You write to the logs using CakeLog::write(). See its documentation for more information.
  55. *
  56. * @package Cake.Log
  57. */
  58. class CakeLog {
  59. /**
  60. * An array of connected streams.
  61. * Each stream represents a callable that will be called when write() is called.
  62. *
  63. * @var array
  64. */
  65. protected static $_streams = array();
  66. /**
  67. * Configure and add a new logging stream to CakeLog
  68. * You can use add loggers from app/Log/Engine use app.loggername, or any plugin/Log/Engine using plugin.loggername.
  69. *
  70. * ### Usage:
  71. *
  72. * {{{
  73. * CakeLog::config('second_file', array(
  74. * 'engine' => 'FileLog',
  75. * 'path' => '/var/logs/my_app/'
  76. * ));
  77. * }}}
  78. *
  79. * Will configure a FileLog instance to use the specified path. All options that are not `engine`
  80. * are passed onto the logging adapter, and handled there. Any class can be configured as a logging
  81. * adapter as long as it implements the methods in CakeLogInterface.
  82. *
  83. * @param string $key The keyname for this logger, used to remove the logger later.
  84. * @param array $config Array of configuration information for the logger
  85. * @return boolean success of configuration.
  86. * @throws CakeLogException
  87. */
  88. public static function config($key, $config) {
  89. if (empty($config['engine'])) {
  90. throw new CakeLogException(__d('cake_dev', 'Missing logger classname'));
  91. }
  92. $loggerName = $config['engine'];
  93. unset($config['engine']);
  94. $className = self::_getLogger($loggerName);
  95. $logger = new $className($config);
  96. if (!$logger instanceof CakeLogInterface) {
  97. throw new CakeLogException(sprintf(
  98. __d('cake_dev', 'logger class %s does not implement a write method.'), $loggerName
  99. ));
  100. }
  101. self::$_streams[$key] = $logger;
  102. return true;
  103. }
  104. /**
  105. * Attempts to import a logger class from the various paths it could be on.
  106. * Checks that the logger class implements a write method as well.
  107. *
  108. * @param string $loggerName the plugin.className of the logger class you want to build.
  109. * @return mixed boolean false on any failures, string of classname to use if search was successful.
  110. * @throws CakeLogException
  111. */
  112. protected static function _getLogger($loggerName) {
  113. list($plugin, $loggerName) = pluginSplit($loggerName, true);
  114. App::uses($loggerName, $plugin . 'Log/Engine');
  115. if (!class_exists($loggerName)) {
  116. throw new CakeLogException(__d('cake_dev', 'Could not load class %s', $loggerName));
  117. }
  118. return $loggerName;
  119. }
  120. /**
  121. * Returns the keynames of the currently active streams
  122. *
  123. * @return array Array of configured log streams.
  124. */
  125. public static function configured() {
  126. return array_keys(self::$_streams);
  127. }
  128. /**
  129. * Removes a stream from the active streams. Once a stream has been removed
  130. * it will no longer have messages sent to it.
  131. *
  132. * @param string $streamName Key name of a configured stream to remove.
  133. * @return void
  134. */
  135. public static function drop($streamName) {
  136. unset(self::$_streams[$streamName]);
  137. }
  138. /**
  139. * Configures the automatic/default stream a FileLog.
  140. *
  141. * @return void
  142. */
  143. protected static function _autoConfig() {
  144. self::_getLogger('FileLog');
  145. self::$_streams['default'] = new FileLog(array('path' => LOGS));
  146. }
  147. /**
  148. * Writes the given message and type to all of the configured log adapters.
  149. * Configured adapters are passed both the $type and $message variables. $type
  150. * is one of the following strings/values.
  151. *
  152. * ### Types:
  153. *
  154. * - `LOG_WARNING` => 'warning',
  155. * - `LOG_NOTICE` => 'notice',
  156. * - `LOG_INFO` => 'info',
  157. * - `LOG_DEBUG` => 'debug',
  158. * - `LOG_ERR` => 'error',
  159. * - `LOG_ERROR` => 'error'
  160. *
  161. * ### Usage:
  162. *
  163. * Write a message to the 'warning' log:
  164. *
  165. * `CakeLog::write('warning', 'Stuff is broken here');`
  166. *
  167. * @param string $type Type of message being written
  168. * @param string $message Message content to log
  169. * @return boolean Success
  170. */
  171. public static function write($type, $message) {
  172. if (!defined('LOG_ERROR')) {
  173. define('LOG_ERROR', 2);
  174. }
  175. if (!defined('LOG_ERR')) {
  176. define('LOG_ERR', LOG_ERROR);
  177. }
  178. $levels = array(
  179. LOG_WARNING => 'warning',
  180. LOG_NOTICE => 'notice',
  181. LOG_INFO => 'info',
  182. LOG_DEBUG => 'debug',
  183. LOG_ERR => 'error',
  184. LOG_ERROR => 'error'
  185. );
  186. if (is_int($type) && isset($levels[$type])) {
  187. $type = $levels[$type];
  188. }
  189. if (empty(self::$_streams)) {
  190. self::_autoConfig();
  191. }
  192. foreach (self::$_streams as $logger) {
  193. $logger->write($type, $message);
  194. }
  195. return true;
  196. }
  197. }