CakeLog.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake.libs
  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. * @package 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. */
  52. protected static $_streams = array();
  53. /**
  54. * Configure and add a new logging stream to CakeLog
  55. * You can use add loggers from app/libs use app.loggername, or any plugin/libs using plugin.loggername.
  56. *
  57. * ### Usage:
  58. *
  59. * {{{
  60. * CakeLog::config('second_file', array(
  61. * 'engine' => 'FileLog',
  62. * 'path' => '/var/logs/my_app/'
  63. * ));
  64. * }}}
  65. *
  66. * Will configure a FileLog instance to use the specified path. All options that are not `engine`
  67. * are passed onto the logging adapter, and handled there. Any class can be configured as a logging
  68. * adapter as long as it implements a `write` method with the following signature.
  69. *
  70. * `write($type, $message)`
  71. *
  72. * For an explaination of these parameters, see CakeLog::write()
  73. *
  74. * @param string $key The keyname for this logger, used to remove the logger later.
  75. * @param array $config Array of configuration information for the logger
  76. * @return boolean success of configuration.
  77. * @throws CakeLogException
  78. */
  79. public static function config($key, $config) {
  80. if (empty($config['engine'])) {
  81. throw new CakeLogException(__d('cake_dev', 'Missing logger classname'));
  82. }
  83. $loggerName = $config['engine'];
  84. unset($config['engine']);
  85. $className = self::_getLogger($loggerName);
  86. $logger = new $className($config);
  87. if (!$logger instanceof CakeLogInterface) {
  88. throw new CakeLogException(sprintf(
  89. __d('cake_dev', 'logger class %s does not implement a write method.'), $loggerName
  90. ));
  91. }
  92. self::$_streams[$key] = $logger;
  93. return true;
  94. }
  95. /**
  96. * Attempts to import a logger class from the various paths it could be on.
  97. * Checks that the logger class implements a write method as well.
  98. *
  99. * @param string $loggerName the plugin.className of the logger class you want to build.
  100. * @return mixed boolean false on any failures, string of classname to use if search was successful.
  101. */
  102. protected static function _getLogger($loggerName) {
  103. list($plugin, $loggerName) = pluginSplit($loggerName, true);
  104. App::uses($loggerName, $plugin . 'Log/Engine');
  105. if (!class_exists($loggerName)) {
  106. throw new CakeLogException(__d('cake_dev', 'Could not load class %s', $loggerName));
  107. }
  108. return $loggerName;
  109. }
  110. /**
  111. * Returns the keynames of the currently active streams
  112. *
  113. * @return array Array of configured log streams.
  114. */
  115. public static function configured() {
  116. return array_keys(self::$_streams);
  117. }
  118. /**
  119. * Removes a stream from the active streams. Once a stream has been removed
  120. * it will no longer have messages sent to it.
  121. *
  122. * @param string $keyname Key name of a configured stream to remove.
  123. * @return void
  124. */
  125. public static function drop($streamName) {
  126. unset(self::$_streams[$streamName]);
  127. }
  128. /**
  129. * Configures the automatic/default stream a FileLog.
  130. *
  131. * @return void
  132. */
  133. protected static function _autoConfig() {
  134. self::_getLogger('FileLog');
  135. self::$_streams['default'] = new FileLog(array('path' => LOGS));
  136. }
  137. /**
  138. * Writes the given message and type to all of the configured log adapters.
  139. * Configured adapters are passed both the $type and $message variables. $type
  140. * is one of the following strings/values.
  141. *
  142. * ### Types:
  143. *
  144. * - `LOG_WARNING` => 'warning',
  145. * - `LOG_NOTICE` => 'notice',
  146. * - `LOG_INFO` => 'info',
  147. * - `LOG_DEBUG` => 'debug',
  148. * - `LOG_ERR` => 'error',
  149. * - `LOG_ERROR` => 'error'
  150. *
  151. * ### Usage:
  152. *
  153. * Write a message to the 'warning' log:
  154. *
  155. * `CakeLog::write('warning', 'Stuff is broken here');`
  156. *
  157. * @param string $type Type of message being written
  158. * @param string $message Message content to log
  159. * @return boolean Success
  160. */
  161. public static function write($type, $message) {
  162. if (!defined('LOG_ERROR')) {
  163. define('LOG_ERROR', 2);
  164. }
  165. if (!defined('LOG_ERR')) {
  166. define('LOG_ERR', LOG_ERROR);
  167. }
  168. $levels = array(
  169. LOG_WARNING => 'warning',
  170. LOG_NOTICE => 'notice',
  171. LOG_INFO => 'info',
  172. LOG_DEBUG => 'debug',
  173. LOG_ERR => 'error',
  174. LOG_ERROR => 'error'
  175. );
  176. if (is_int($type) && isset($levels[$type])) {
  177. $type = $levels[$type];
  178. }
  179. if (empty(self::$_streams)) {
  180. self::_autoConfig();
  181. }
  182. foreach (self::$_streams as $key => $logger) {
  183. $logger->write($type, $message);
  184. }
  185. return true;
  186. }
  187. }