| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfFileLogger extends sfLogger |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$type = 'symfony', |
|---|
| 23 |
$format = '%time% %type% [%priority%] %message%%EOL%', |
|---|
| 24 |
$timeFormat = '%b %d %H:%M:%S', |
|---|
| 25 |
$fp = null; |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
* Initializes this logger. |
|---|
| 29 |
* |
|---|
| 30 |
* Available options: |
|---|
| 31 |
* |
|---|
| 32 |
* - file: The file path or a php wrapper to log messages |
|---|
| 33 |
* You can use any support php wrapper. To write logs to the Apache error log, use php://stderr |
|---|
| 34 |
* - format: The log line format (default to %time% %type% [%priority%] %message%%EOL%) |
|---|
| 35 |
* - time_format: The log time strftime format (default to %b %d %H:%M:%S) |
|---|
| 36 |
* - dir_mode: The mode to use when creating a directory (default to 0777) |
|---|
| 37 |
* - file_mode: The mode to use when creating a file (default to 0666) |
|---|
| 38 |
* |
|---|
| 39 |
* @param sfEventDispatcher $dispatcher A sfEventDispatcher instance |
|---|
| 40 |
* @param array $options An array of options. |
|---|
| 41 |
* |
|---|
| 42 |
* @return Boolean true, if initialization completes successfully, otherwise false. |
|---|
| 43 |
*/ |
|---|
| 44 |
public function initialize(sfEventDispatcher $dispatcher, $options = array()) |
|---|
| 45 |
{ |
|---|
| 46 |
if (!isset($options['file'])) |
|---|
| 47 |
{ |
|---|
| 48 |
throw new sfConfigurationException('You must provide a "file" parameter for this logger.'); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
if (isset($options['format'])) |
|---|
| 52 |
{ |
|---|
| 53 |
$this->format = $options['format']; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
if (isset($options['time_format'])) |
|---|
| 57 |
{ |
|---|
| 58 |
$this->timeFormat = $options['time_format']; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
if (isset($options['type'])) |
|---|
| 62 |
{ |
|---|
| 63 |
$this->type = $options['type']; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
$dir = dirname($options['file']); |
|---|
| 67 |
if (!is_dir($dir)) |
|---|
| 68 |
{ |
|---|
| 69 |
mkdir($dir, isset($options['dir_mode']) ? $options['dir_mode'] : 0777, true); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
$fileExists = file_exists($options['file']); |
|---|
| 73 |
if (!is_writable($dir) || ($fileExists && !is_writable($options['file']))) |
|---|
| 74 |
{ |
|---|
| 75 |
throw new sfFileException(sprintf('Unable to open the log file "%s" for writing.', $options['file'])); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
$this->fp = fopen($options['file'], 'a'); |
|---|
| 79 |
if (!$fileExists) |
|---|
| 80 |
{ |
|---|
| 81 |
chmod($options['file'], isset($options['file_mode']) ? $options['file_mode'] : 0666); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
return parent::initialize($dispatcher, $options); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
* Logs a message. |
|---|
| 89 |
* |
|---|
| 90 |
* @param string $message Message |
|---|
| 91 |
* @param string $priority Message priority |
|---|
| 92 |
*/ |
|---|
| 93 |
protected function doLog($message, $priority) |
|---|
| 94 |
{ |
|---|
| 95 |
flock($this->fp, LOCK_EX); |
|---|
| 96 |
fwrite($this->fp, strtr($this->format, array( |
|---|
| 97 |
'%type%' => $this->type, |
|---|
| 98 |
'%message%' => $message, |
|---|
| 99 |
'%time%' => strftime($this->timeFormat), |
|---|
| 100 |
'%priority%' => $this->getPriority($priority), |
|---|
| 101 |
'%EOL%' => PHP_EOL, |
|---|
| 102 |
))); |
|---|
| 103 |
flock($this->fp, LOCK_UN); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
* Returns the priority string to use in log messages. |
|---|
| 108 |
* |
|---|
| 109 |
* @param string $priority The priority constant |
|---|
| 110 |
* |
|---|
| 111 |
* @return string The priority to use in log messages |
|---|
| 112 |
*/ |
|---|
| 113 |
protected function getPriority($priority) |
|---|
| 114 |
{ |
|---|
| 115 |
return sfLogger::getPriorityName($priority); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
* Executes the shutdown method. |
|---|
| 120 |
*/ |
|---|
| 121 |
public function shutdown() |
|---|
| 122 |
{ |
|---|
| 123 |
if (is_resource($this->fp)) |
|---|
| 124 |
{ |
|---|
| 125 |
fclose($this->fp); |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|