[ Index ] |
|
Code source de SPIP Agora 1.4 |
1 <?php 2 /** 3 * $Header: /repository/pear/Log/Log/file.php,v 1.37 2004/01/19 08:02:40 jon Exp $ 4 * 5 * @version $Revision: 1.37 $ 6 * @package Log 7 */ 8 9 /** 10 * The Log_file class is a concrete implementation of the Log abstract 11 * class that logs messages to a text file. 12 * 13 * @author Jon Parise <jon@php.net> 14 * @author Roman Neuhauser <neuhauser@bellavista.cz> 15 * @since Log 1.0 16 * @package Log 17 * 18 * @example file.php Using the file handler. 19 */ 20 class Log_file extends Log 21 { 22 /** 23 * String containing the name of the log file. 24 * @var string 25 * @access private 26 */ 27 var $_filename = 'php.log'; 28 29 /** 30 * Handle to the log file. 31 * @var resource 32 * @access private 33 */ 34 var $_fp = false; 35 36 /** 37 * Should new log entries be append to an existing log file, or should the 38 * a new log file overwrite an existing one? 39 * @var boolean 40 * @access private 41 */ 42 var $_append = true; 43 44 /** 45 * Integer (in octal) containing the log file's permissions mode. 46 * @var integer 47 * @access private 48 */ 49 var $_mode = 0644; 50 51 /** 52 * String containing the format of a log line. 53 * @var string 54 * @access private 55 */ 56 var $_lineFormat = '%1$s %2$s [%3$s] %4$s'; 57 58 /** 59 * String containing the timestamp format. It will be passed directly to 60 * strftime(). Note that the timestamp string will generated using the 61 * current locale. 62 * @var string 63 * @access private 64 */ 65 var $_timeFormat = '%b %d %H:%M:%S'; 66 67 /** 68 * Hash that maps canonical format keys to position arguments for the 69 * "line format" string. 70 * @var array 71 * @access private 72 */ 73 var $_formatMap = array('%{timestamp}' => '%1$s', 74 '%{ident}' => '%2$s', 75 '%{priority}' => '%3$s', 76 '%{message}' => '%4$s', 77 '%\{' => '%%{'); 78 79 /** 80 * String containing the end-on-line character sequence. 81 * @var string 82 * @access private 83 */ 84 var $_eol = "\n"; 85 86 /** 87 * Constructs a new Log_file object. 88 * 89 * @param string $name Ignored. 90 * @param string $ident The identity string. 91 * @param array $conf The configuration array. 92 * @param int $level Log messages up to and including this level. 93 * @access public 94 */ 95 function Log_file($name, $ident = '', $conf = array(), 96 $level = PEAR_LOG_DEBUG) 97 { 98 $this->_id = md5(microtime()); 99 $this->_filename = $name; 100 $this->_ident = $ident; 101 $this->_mask = Log::UPTO($level); 102 103 if (isset($conf['append'])) { 104 $this->_append = $conf['append']; 105 } 106 107 if (!empty($conf['mode'])) { 108 $this->_mode = $conf['mode']; 109 } 110 111 if (!empty($conf['lineFormat'])) { 112 $this->_lineFormat = str_replace(array_keys($this->_formatMap), 113 array_values($this->_formatMap), 114 $conf['lineFormat']); 115 } 116 117 if (!empty($conf['timeFormat'])) { 118 $this->_timeFormat = $conf['timeFormat']; 119 } 120 121 if (!empty($conf['eol'])) { 122 $this->_eol = $conf['eol']; 123 } else { 124 $this->_eol = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n"; 125 } 126 127 register_shutdown_function(array(&$this, '_Log_file')); 128 } 129 130 /** 131 * Destructor 132 */ 133 function _Log_file() 134 { 135 if ($this->_opened) { 136 $this->close(); 137 } 138 } 139 140 /** 141 * Creates the given directory path. If the parent directories don't 142 * already exist, they will be created, too. 143 * 144 * @param string $path The full directory path to create. 145 * @param integer $mode The permissions mode with which the 146 * directories will be created. 147 * 148 * @return True if the full path is successfully created or already 149 * exists. 150 * 151 * @access private 152 */ 153 function _mkpath($path, $mode = 0700) 154 { 155 static $depth = 0; 156 157 /* Guard against potentially infinite recursion. */ 158 if ($depth++ > 25) { 159 trigger_error("_mkpath(): Maximum recursion depth (25) exceeded", 160 E_USER_WARNING); 161 return false; 162 } 163 164 /* We're only interested in the directory component of the path. */ 165 $path = dirname($path); 166 167 /* If the directory already exists, return success immediately. */ 168 if (is_dir($path)) { 169 $depth = 0; 170 return true; 171 } 172 173 /* 174 * In order to understand recursion, you must first understand 175 * recursion ... 176 */ 177 if ($this->_mkpath($path, $mode) === false) { 178 return false; 179 } 180 181 return @mkdir($path, $mode); 182 } 183 184 /** 185 * Opens the log file for output. If the specified log file does not 186 * already exist, it will be created. By default, new log entries are 187 * appended to the end of the log file. 188 * 189 * This is implicitly called by log(), if necessary. 190 * 191 * @access public 192 */ 193 function open() 194 { 195 if (!$this->_opened) { 196 /* If the log file's directory doesn't exist, create it. */ 197 if (!is_dir(dirname($this->_filename))) { 198 $this->_mkpath($this->_filename); 199 } 200 201 /* Obtain a handle to the log file. */ 202 $this->_fp = fopen($this->_filename, ($this->_append) ? 'a' : 'w'); 203 204 $this->_opened = ($this->_fp !== false); 205 206 /* Attempt to set the log file's mode. */ 207 @chmod($this->_filename, $this->_mode); 208 } 209 210 return $this->_opened; 211 } 212 213 /** 214 * Closes the log file if it is open. 215 * 216 * @access public 217 */ 218 function close() 219 { 220 /* If the log file is open, close it. */ 221 if ($this->_opened && fclose($this->_fp)) { 222 $this->_opened = false; 223 } 224 225 return ($this->_opened === false); 226 } 227 228 /** 229 * Flushes all pending data to the file handle. 230 * 231 * @access public 232 * @since Log 1.8.2 233 */ 234 function flush() 235 { 236 return fflush($this->_fp); 237 } 238 239 /** 240 * Logs $message to the output window. The message is also passed along 241 * to any Log_observer instances that are observing this Log. 242 * 243 * @param mixed $message String or object containing the message to log. 244 * @param string $priority The priority of the message. Valid 245 * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT, 246 * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING, 247 * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG. 248 * @return boolean True on success or false on failure. 249 * @access public 250 */ 251 function log($message, $priority = null) 252 { 253 /* If a priority hasn't been specified, use the default value. */ 254 if ($priority === null) { 255 $priority = $this->_priority; 256 } 257 258 /* Abort early if the priority is above the maximum logging level. */ 259 if (!$this->_isMasked($priority)) { 260 return false; 261 } 262 263 /* If the log file isn't already open, open it now. */ 264 if (!$this->_opened && !$this->open()) { 265 return false; 266 } 267 268 /* Extract the string representation of the message. */ 269 $message = $this->_extractMessage($message); 270 271 /* Build the string containing the complete log line. */ 272 $line = sprintf($this->_lineFormat, strftime($this->_timeFormat), 273 $this->_ident, $this->priorityToString($priority), 274 $message) . $this->_eol; 275 276 /* Write the log line to the log file. */ 277 $success = (fwrite($this->_fp, $line) !== false); 278 279 /* Notify observers about this log message. */ 280 $this->_announce(array('priority' => $priority, 'message' => $message)); 281 282 return $success; 283 } 284 } 285 286 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 14:40:03 2007 | par Balluche grâce à PHPXref 0.7 |