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