1. sfLogger.class.php
  2. /** * sfLogger is the abstract class for all logging classes. * * This level list is ordered by highest priority (self::EMERG) to lowest priority (self::DEBUG): * - EMERG: System is unusable * - ALERT: Immediate action required * - CRIT: Critical conditions * - ERR: Error conditions * - WARNING: Warning conditions * - NOTICE: Normal but significant * - INFO: Informational * - DEBUG: Debug-level messages * * @package symfony * @subpackage log * @author Fabien Potencier * @version SVN: $Id: sfLogger.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. abstract class sfLogger
  4. {
  5. const EMERG = 0; // System is unusable
  6. const ALERT = 1; // Immediate action required
  7. const CRIT = 2; // Critical conditions
  8. const ERR = 3; // Error conditions
  9. const WARNING = 4; // Warning conditions
  10. const NOTICE = 5; // Normal but significant
  11. const INFO = 6; // Informational
  12. const DEBUG = 7; // Debug-level messages
  13. protected
  14. $dispatcher = null,
  15. $options = array(),
  16. $level = self::INFO;
  17. /**
  18. * Class constructor.
  19. *
  20. * @see initialize()
  21. */
  22. public function __construct(sfEventDispatcher $dispatcher, $options = array())
  23. {
  24. $this->initialize($dispatcher, $options);
  25. if (!isset($options['auto_shutdown']) || $options['auto_shutdown'])
  26. {
  27. register_shutdown_function(array($this, 'shutdown'));
  28. }
  29. }
  30. /**
  31. * Initializes this sfLogger instance.
  32. *
  33. * Available options:
  34. *
  35. * - level: The log level.
  36. *
  37. * @param sfEventDispatcher $dispatcher A sfEventDispatcher instance
  38. * @param array $options An array of options.
  39. *
  40. * @return Boolean true, if initialization completes successfully, otherwise false.
  41. *
  42. * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfLogger.
  43. */
  44. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  45. {
  46. $this->dispatcher = $dispatcher;
  47. $this->options = $options;
  48. if (isset($this->options['level']))
  49. {
  50. $this->setLogLevel($this->options['level']);
  51. }
  52. $dispatcher->connect('application.log', array($this, 'listenToLogEvent'));
  53. }
  54. /**
  55. * Returns the options for the logger instance.
  56. */
  57. public function getOptions()
  58. {
  59. return $this->options;
  60. }
  61. /**
  62. * Returns the options for the logger instance.
  63. */
  64. public function setOption($name, $value)
  65. {
  66. $this->options[$name] = $value;
  67. }
  68. /**
  69. * Retrieves the log level for the current logger instance.
  70. *
  71. * @return string Log level
  72. */
  73. public function getLogLevel()
  74. {
  75. return $this->level;
  76. }
  77. /**
  78. * Sets a log level for the current logger instance.
  79. *
  80. * @param string $level Log level
  81. */
  82. public function setLogLevel($level)
  83. {
  84. if (!is_int($level))
  85. {
  86. $level = constant('sfLogger::'.strtoupper($level));
  87. }
  88. $this->level = $level;
  89. }
  90. /**
  91. * Logs a message.
  92. *
  93. * @param string $message Message
  94. * @param string $priority Message priority
  95. */
  96. public function log($message, $priority = self::INFO)
  97. {
  98. if ($this->getLogLevel() < $priority)
  99. {
  100. return false;
  101. }
  102. return $this->doLog($message, $priority);
  103. }
  104. /**
  105. * Logs a message.
  106. *
  107. * @param string $message Message
  108. * @param string $priority Message priority
  109. */
  110. abstract protected function doLog($message, $priority);
  111. /**
  112. * Logs an emerg message.
  113. *
  114. * @param string $message Message
  115. */
  116. public function emerg($message)
  117. {
  118. $this->log($message, self::EMERG);
  119. }
  120. /**
  121. * Logs an alert message.
  122. *
  123. * @param string $message Message
  124. */
  125. public function alert($message)
  126. {
  127. $this->log($message, self::ALERT);
  128. }
  129. /**
  130. * Logs a critical message.
  131. *
  132. * @param string $message Message
  133. */
  134. public function crit($message)
  135. {
  136. $this->log($message, self::CRIT);
  137. }
  138. /**
  139. * Logs an error message.
  140. *
  141. * @param string $message Message
  142. */
  143. public function err($message)
  144. {
  145. $this->log($message, self::ERR);
  146. }
  147. /**
  148. * Logs a warning message.
  149. *
  150. * @param string $message Message
  151. */
  152. public function warning($message)
  153. {
  154. $this->log($message, self::WARNING);
  155. }
  156. /**
  157. * Logs a notice message.
  158. *
  159. * @param string $message Message
  160. */
  161. public function notice($message)
  162. {
  163. $this->log($message, self::NOTICE);
  164. }
  165. /**
  166. * Logs an info message.
  167. *
  168. * @param string $message Message
  169. */
  170. public function info($message)
  171. {
  172. $this->log($message, self::INFO);
  173. }
  174. /**
  175. * Logs a debug message.
  176. *
  177. * @param string $message Message
  178. */
  179. public function debug($message)
  180. {
  181. $this->log($message, self::DEBUG);
  182. }
  183. /**
  184. * Listens to application.log events.
  185. *
  186. * @param sfEvent $event An sfEvent instance
  187. */
  188. public function listenToLogEvent(sfEvent $event)
  189. {
  190. $priority = isset($event['priority']) ? $event['priority'] : self::INFO;
  191. $subject = $event->getSubject();
  192. $subject = is_object($subject) ? get_class($subject) : (is_string($subject) ? $subject : 'main');
  193. foreach ($event->getParameters() as $key => $message)
  194. {
  195. if ('priority' === $key)
  196. {
  197. continue;
  198. }
  199. $this->log(sprintf('{%s} %s', $subject, $message), $priority);
  200. }
  201. }
  202. /**
  203. * Executes the shutdown procedure.
  204. *
  205. * Cleans up the current logger instance.
  206. */
  207. public function shutdown()
  208. {
  209. }
  210. /**
  211. * Returns the priority name given a priority class constant
  212. *
  213. * @param integer $priority A priority class constant
  214. *
  215. * @return string The priority name
  216. *
  217. * @throws sfException if the priority level does not exist
  218. */
  219. static public function getPriorityName($priority)
  220. {
  221. static $levels = array(
  222. self::EMERG => 'emerg',
  223. self::ALERT => 'alert',
  224. self::CRIT => 'crit',
  225. self::ERR => 'err',
  226. self::WARNING => 'warning',
  227. self::NOTICE => 'notice',
  228. self::INFO => 'info',
  229. self::DEBUG => 'debug',
  230. );
  231. if (!isset($levels[$priority]))
  232. {
  233. throw new sfException(sprintf('The priority level "%s" does not exist.', $priority));
  234. }
  235. return $levels[$priority];
  236. }
  237. }

Debug toolbar