123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- abstract class sfLogger
- {
- const EMERG = 0;
- const ALERT = 1;
- const CRIT = 2;
- const ERR = 3;
- const WARNING = 4;
- const NOTICE = 5;
- const INFO = 6;
- const DEBUG = 7;
- protected
- $level = self::INFO;
-
- public function __construct(sfEventDispatcher $dispatcher, $options = array())
- {
- $this->initialize($dispatcher, $options);
- if (!isset($options['auto_shutdown']) || $options['auto_shutdown'])
- {
- register_shutdown_function(array($this, 'shutdown'));
- }
- }
-
- public function initialize(sfEventDispatcher $dispatcher, $options = array())
- {
- if (isset($options['level']))
- {
- $this->setLogLevel($options['level']);
- }
- $dispatcher->connect('application.log', array($this, 'listenToLogEvent'));
- }
-
- public function getLogLevel()
- {
- return $this->level;
- }
-
- public function setLogLevel($level)
- {
- if (!is_int($level))
- {
- $level = constant('sfLogger::'.strtoupper($level));
- }
- $this->level = $level;
- }
-
- public function log($message, $priority = self::INFO)
- {
- if ($this->getLogLevel() < $priority)
- {
- return false;
- }
- return $this->doLog($message, $priority);
- }
-
- abstract protected function doLog($message, $priority);
-
- public function emerg($message)
- {
- $this->log($message, self::EMERG);
- }
-
- public function alert($message)
- {
- $this->log($message, self::ALERT);
- }
-
- public function crit($message)
- {
- $this->log($message, self::CRIT);
- }
-
- public function err($message)
- {
- $this->log($message, self::ERR);
- }
-
- public function warning($message)
- {
- $this->log($message, self::WARNING);
- }
-
- public function notice($message)
- {
- $this->log($message, self::NOTICE);
- }
-
- public function info($message)
- {
- $this->log($message, self::INFO);
- }
-
- public function debug($message)
- {
- $this->log($message, self::DEBUG);
- }
-
- public function listenToLogEvent(sfEvent $event)
- {
- $priority = isset($event['priority']) ? $event['priority'] : self::INFO;
- $subject = $event->getSubject();
- $subject = is_object($subject) ? get_class($subject) : (is_string($subject) ? $subject : 'main');
- foreach ($event->getParameters() as $key => $message)
- {
- if ('priority' === $key)
- {
- continue;
- }
- $this->log(sprintf('{%s} %s', $subject, $message), $priority);
- }
- }
-
- public function shutdown()
- {
- }
-
- static public function getPriorityName($priority)
- {
- static $levels = array(
- self::EMERG => 'emerg',
- self::ALERT => 'alert',
- self::CRIT => 'crit',
- self::ERR => 'err',
- self::WARNING => 'warning',
- self::NOTICE => 'notice',
- self::INFO => 'info',
- self::DEBUG => 'debug',
- );
- if (!isset($levels[$priority]))
- {
- throw new sfException(sprintf('The priority level "%s" does not exist.', $priority));
- }
- return $levels[$priority];
- }
- }
|