sfLogger.class.php 6.0 KB

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