Development

/branches/1.2/lib/log/sfLogger.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/log/sfLogger.class.php

Revision 16344, 5.9 kB (checked in by fabien, 5 years ago)

[1.0, 1.1, 1.2, 1.3] replaced this->level usage by this->getLogLevel (closes #6086)

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