PageRenderTime 110ms CodeModel.GetById 48ms RepoModel.GetById 2ms app.codeStats 0ms

/cake/libs/cake_log.php

https://github.com/msadouni/cakephp2x
PHP | 236 lines | 127 code | 13 blank | 96 comment | 21 complexity | 92990ebea58825ba41c0441e190b9a74 MD5 | raw file
  1. <?php
  2. /**
  3. * Logging.
  4. *
  5. * Log messages to text files.
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2009, 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-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.libs
  19. * @since CakePHP(tm) v 0.2.9
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. /**
  23. * Set up error level constants to be used within the framework if they are not defined within the
  24. * system.
  25. *
  26. */
  27. if (!defined('LOG_WARNING')) {
  28. define('LOG_WARNING', 3);
  29. }
  30. if (!defined('LOG_NOTICE')) {
  31. define('LOG_NOTICE', 4);
  32. }
  33. if (!defined('LOG_DEBUG')) {
  34. define('LOG_DEBUG', 5);
  35. }
  36. if (!defined('LOG_INFO')) {
  37. define('LOG_INFO', 6);
  38. }
  39. /**
  40. * Logs messages to text files
  41. *
  42. * @package cake
  43. * @subpackage cake.cake.libs
  44. */
  45. class CakeLog {
  46. /**
  47. * An array of connected streams.
  48. * Each stream represents a callable that will be called when write() is called.
  49. *
  50. * @var array
  51. * @access protected
  52. * @static
  53. */
  54. protected static $_streams = array();
  55. /**
  56. * Configure and add a new logging stream to CakeLog
  57. * You can use add loggers from app/libs use app.loggername, or any plugin/libs using plugin.loggername
  58. *
  59. * @param string $key The keyname for this logger, used to revmoe the logger later.
  60. * @param array $config Array of configuration information for the logger
  61. * @return boolean success of configuration.
  62. * @static
  63. */
  64. public static function config($key, $config) {
  65. if (empty($config['engine'])) {
  66. trigger_error(__('Missing logger classname', true), E_USER_WARNING);
  67. return false;
  68. }
  69. $className = self::_getLogger($config['engine']);
  70. if (!$className) {
  71. return false;
  72. }
  73. unset($config['engine']);
  74. self::$_streams[$key] = new $className($config);
  75. return true;
  76. }
  77. /**
  78. * Attempts to import a logger class from the various paths it could be on.
  79. * Checks that the logger class implements a write method as well.
  80. *
  81. * @return mixed boolean false on any failures, string of classname to use if search was successful.\
  82. * @access protected
  83. */
  84. protected function _getLogger($loggerName) {
  85. list($plugin, $loggerName) = pluginSplit($loggerName);
  86. if ($plugin) {
  87. App::import('Lib', $plugin . '.log/' . $loggerName);
  88. } else {
  89. if (!App::import('Lib', 'log/' . $loggerName)) {
  90. App::import('Core', 'log/' . $loggerName);
  91. }
  92. }
  93. if (!class_exists($loggerName)) {
  94. trigger_error(sprintf(__('Could not load logger class %s', true), $loggerName), E_USER_WARNING);
  95. return false;
  96. }
  97. if (!method_exists($loggerName, 'write')) {
  98. trigger_error(
  99. sprintf(__('logger class %s does not implement a write method.', true), $loggerName),
  100. E_USER_WARNING
  101. );
  102. return false;
  103. }
  104. return $loggerName;
  105. }
  106. /**
  107. * Returns the keynames of the currently active streams
  108. *
  109. * @return array
  110. * @access public
  111. * @static
  112. */
  113. public function configured() {
  114. return array_keys(self::$_streams);
  115. }
  116. /**
  117. * Removes a stream from the active streams. Once a stream has been removed
  118. * it will no longer have messages sent to it.
  119. *
  120. * @param string $keyname Key name of callable to remove.
  121. * @return void
  122. * @access public
  123. * @static
  124. */
  125. public function drop($streamName) {
  126. unset(self::$_streams[$streamName]);
  127. }
  128. /**
  129. * Configures the automatic/default stream a FileLog.
  130. *
  131. * @return void
  132. * @access protected
  133. */
  134. protected static function _autoConfig() {
  135. if (!class_exists('FileLog')) {
  136. App::import('Core', 'log/FileLog');
  137. }
  138. self::$_streams['default'] = new FileLog(array('path' => LOGS));
  139. }
  140. /**
  141. * Writes given message to a log file in the logs directory.
  142. *
  143. * @param string $type Type of log, becomes part of the log's filename
  144. * @param string $message Message to log
  145. * @return boolean Success
  146. * @access public
  147. * @static
  148. */
  149. public static function write($type, $message) {
  150. if (!defined('LOG_ERROR')) {
  151. define('LOG_ERROR', 2);
  152. }
  153. if (!defined('LOG_ERR')) {
  154. define('LOG_ERR', LOG_ERROR);
  155. }
  156. $levels = array(
  157. LOG_WARNING => 'warning',
  158. LOG_NOTICE => 'notice',
  159. LOG_INFO => 'info',
  160. LOG_DEBUG => 'debug',
  161. LOG_ERR => 'error',
  162. LOG_ERROR => 'error'
  163. );
  164. if (is_int($type) && isset($levels[$type])) {
  165. $type = $levels[$type];
  166. }
  167. if (empty(self::$_streams)) {
  168. self::_autoConfig();
  169. }
  170. $keys = array_keys(self::$_streams);
  171. foreach ($keys as $key) {
  172. $logger = self::$_streams[$key];
  173. $logger->write($type, $message);
  174. }
  175. return true;
  176. }
  177. /**
  178. * An error_handler that will log errors to file using CakeLog::write();
  179. *
  180. * @param integer $code Code of error
  181. * @param string $description Error description
  182. * @param string $file File on which error occurred
  183. * @param integer $line Line that triggered the error
  184. * @param array $context Context
  185. * @return void
  186. */
  187. public static function handleError($code, $description, $file = null, $line = null, $context = null) {
  188. if ($code === 2048 || $code === 8192) {
  189. return;
  190. }
  191. switch ($code) {
  192. case E_PARSE:
  193. case E_ERROR:
  194. case E_CORE_ERROR:
  195. case E_COMPILE_ERROR:
  196. case E_USER_ERROR:
  197. $error = 'Fatal Error';
  198. $level = LOG_ERROR;
  199. break;
  200. case E_WARNING:
  201. case E_USER_WARNING:
  202. case E_COMPILE_WARNING:
  203. case E_RECOVERABLE_ERROR:
  204. $error = 'Warning';
  205. $level = LOG_WARNING;
  206. break;
  207. case E_NOTICE:
  208. case E_USER_NOTICE:
  209. $error = 'Notice';
  210. $level = LOG_NOTICE;
  211. break;
  212. default:
  213. return;
  214. break;
  215. }
  216. $message = $error . ' (' . $code . '): ' . $description . ' in [' . $file . ', line ' . $line . ']';
  217. self::write($level, $message);
  218. }
  219. }
  220. if (!defined('DISABLE_DEFAULT_ERROR_HANDLING')) {
  221. set_error_handler(array('CakeLog', 'handleError'));
  222. }
  223. ?>