5 * @version $Revision: 224513 $
10 * The Log_file class is a concrete implementation of the Log abstract
11 * class that logs messages to a text file.
13 * @author Jon Parise <jon@php.net>
14 * @author Roman Neuhauser <neuhauser@bellavista.cz>
18 * @example file.php Using the file handler.
20 class Log_file
extends Log
23 * String containing the name of the log file.
27 var $_filename = 'php.log';
30 * Handle to the log file.
37 * Should new log entries be append to an existing log file, or should the
38 * a new log file overwrite an existing one?
45 * Should advisory file locking (i.e., flock()) be used?
49 var $_locking = false;
52 * Integer (in octal) containing the log file's permissions mode.
59 * Integer (in octal) specifying the file permission mode that will be
60 * used when creating directories that do not already exist.
67 * String containing the format of a log line.
71 var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
74 * String containing the timestamp format. It will be passed directly to
75 * strftime(). Note that the timestamp string will generated using the
80 var $_timeFormat = '%b %d %H:%M:%S';
83 * String containing the end-on-line character sequence.
90 * Constructs a new Log_file object.
92 * @param string $name Ignored.
93 * @param string $ident The identity string.
94 * @param array $conf The configuration array.
95 * @param int $level Log messages up to and including this level.
98 function Log_file($name, $ident = '', $conf = array(),
99 $level = PEAR_LOG_DEBUG
)
101 $this->_id
= md5(microtime());
102 $this->_filename
= $name;
103 $this->_ident
= $ident;
104 $this->_mask
= Log
::UPTO($level);
106 if (isset($conf['append'])) {
107 $this->_append
= $conf['append'];
110 if (isset($conf['locking'])) {
111 $this->_locking
= $conf['locking'];
114 if (!empty($conf['mode'])) {
115 if (is_string($conf['mode'])) {
116 $this->_mode
= octdec($conf['mode']);
118 $this->_mode
= $conf['mode'];
122 if (!empty($conf['dirmode'])) {
123 if (is_string($conf['dirmode'])) {
124 $this->_dirmode
= octdec($conf['dirmode']);
126 $this->_dirmode
= $conf['dirmode'];
130 if (!empty($conf['lineFormat'])) {
131 $this->_lineFormat
= str_replace(array_keys($this->_formatMap
),
132 array_values($this->_formatMap
),
133 $conf['lineFormat']);
136 if (!empty($conf['timeFormat'])) {
137 $this->_timeFormat
= $conf['timeFormat'];
140 if (!empty($conf['eol'])) {
141 $this->_eol
= $conf['eol'];
143 $this->_eol
= (strstr(PHP_OS
, 'WIN')) ?
"\r\n" : "\n";
146 register_shutdown_function(array(&$this, '_Log_file'));
154 if ($this->_opened
) {
160 * Creates the given directory path. If the parent directories don't
161 * already exist, they will be created, too.
163 * This implementation is inspired by Python's os.makedirs function.
165 * @param string $path The full directory path to create.
166 * @param integer $mode The permissions mode with which the
167 * directories will be created.
169 * @return True if the full path is successfully created or already
174 function _mkpath($path, $mode = 0700)
176 /* Separate the last pathname component from the rest of the path. */
177 $head = dirname($path);
178 $tail = basename($path);
180 /* Make sure we've split the path into two complete components. */
182 $head = dirname($path);
183 $tail = basename($path);
186 /* Recurse up the path if our current segment does not exist. */
187 if (!empty($head) && !empty($tail) && !is_dir($head)) {
188 $this->_mkpath($head, $mode);
191 /* Create this segment of the path. */
192 return @mkdir
($head, $mode);
196 * Opens the log file for output. If the specified log file does not
197 * already exist, it will be created. By default, new log entries are
198 * appended to the end of the log file.
200 * This is implicitly called by log(), if necessary.
206 if (!$this->_opened
) {
207 /* If the log file's directory doesn't exist, create it. */
208 if (!is_dir(dirname($this->_filename
))) {
209 $this->_mkpath($this->_filename
, $this->_dirmode
);
212 /* Determine whether the log file needs to be created. */
213 $creating = !file_exists($this->_filename
);
215 /* Obtain a handle to the log file. */
216 $this->_fp
= fopen($this->_filename
, ($this->_append
) ?
'a' : 'w');
218 /* We consider the file "opened" if we have a valid file pointer. */
219 $this->_opened
= ($this->_fp
!== false);
221 /* Attempt to set the file's permissions if we just created it. */
222 if ($creating && $this->_opened
) {
223 chmod($this->_filename
, $this->_mode
);
227 return $this->_opened
;
231 * Closes the log file if it is open.
237 /* If the log file is open, close it. */
238 if ($this->_opened
&& fclose($this->_fp
)) {
239 $this->_opened
= false;
242 return ($this->_opened
=== false);
246 * Flushes all pending data to the file handle.
253 if (is_resource($this->_fp
)) {
254 return fflush($this->_fp
);
261 * Logs $message to the output window. The message is also passed along
262 * to any Log_observer instances that are observing this Log.
264 * @param mixed $message String or object containing the message to log.
265 * @param string $priority The priority of the message. Valid
266 * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
267 * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
268 * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
269 * @return boolean True on success or false on failure.
272 function log($message, $priority = null)
274 /* If a priority hasn't been specified, use the default value. */
275 if ($priority === null) {
276 $priority = $this->_priority
;
279 /* Abort early if the priority is above the maximum logging level. */
280 if (!$this->_isMasked($priority)) {
284 /* If the log file isn't already open, open it now. */
285 if (!$this->_opened
&& !$this->open()) {
289 /* Extract the string representation of the message. */
290 $message = $this->_extractMessage($message);
292 /* Build the string containing the complete log line. */
293 $line = $this->_format($this->_lineFormat
,
294 strftime($this->_timeFormat
),
295 $priority, $message) . $this->_eol
;
297 /* If locking is enabled, acquire an exclusive lock on the file. */
298 if ($this->_locking
) {
299 flock($this->_fp
, LOCK_EX
);
302 /* Write the log line to the log file. */
303 $success = (fwrite($this->_fp
, $line) !== false);
305 /* Unlock the file now that we're finished writing to it. */
306 if ($this->_locking
) {
307 flock($this->_fp
, LOCK_UN
);
310 /* Notify observers about this log message. */
311 $this->_announce(array('priority' => $priority, 'message' => $message));