ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
Log_file Class Reference
+ Inheritance diagram for Log_file:
+ Collaboration diagram for Log_file:

Public Member Functions

 Log_file ($name, $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
 Constructs a new Log_file object. More...
 
 _Log_file ()
 Destructor. More...
 
 _mkpath ($path, $mode=0700)
 Creates the given directory path. More...
 
 open ()
 Opens the log file for output. More...
 
 close ()
 Closes the log file if it is open. More...
 
 flush ()
 Flushes all pending data to the file handle. More...
 
 log ($message, $priority=null)
 Logs $message to the output window. More...
 
- Public Member Functions inherited from Log
 _classExists ($class)
 Utility function which wraps PHP's class_exists() function to ensure consistent behavior between PHP versions 4 and 5. More...
 
factory ($handler, $name='', $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
 Attempts to return a concrete Log instance of type $handler. More...
 
singleton ($handler, $name='', $ident='', $conf=array(), $level=PEAR_LOG_DEBUG)
 Attempts to return a reference to a concrete Log instance of type $handler, only creating a new instance if no log instance with the same parameters currently exists. More...
 
 open ()
 Abstract implementation of the open() method. More...
 
 close ()
 Abstract implementation of the close() method. More...
 
 flush ()
 Abstract implementation of the flush() method. More...
 
 log ($message, $priority=null)
 Abstract implementation of the log() method. More...
 
 emerg ($message)
 A convenience function for logging a emergency event. More...
 
 alert ($message)
 A convenience function for logging an alert event. More...
 
 crit ($message)
 A convenience function for logging a critical event. More...
 
 err ($message)
 A convenience function for logging a error event. More...
 
 warning ($message)
 A convenience function for logging a warning event. More...
 
 notice ($message)
 A convenience function for logging a notice event. More...
 
 info ($message)
 A convenience function for logging a information event. More...
 
 debug ($message)
 A convenience function for logging a debug event. More...
 
 _extractMessage ($message)
 Returns the string representation of the message data. More...
 
 _getBacktraceVars ($depth)
 Using debug_backtrace(), returns the file, line, and enclosing function name of the source code context from which log() was invoked. More...
 
 _format ($format, $timestamp, $priority, $message)
 Produces a formatted log line based on a format string and a set of variables representing the current log record and state. More...
 
 priorityToString ($priority)
 Returns the string representation of a PEAR_LOG_* integer constant. More...
 
 stringToPriority ($name)
 Returns the the PEAR_LOG_* integer constant for the given string representation of a priority name. More...
 
 MASK ($priority)
 Calculate the log mask for the given priority. More...
 
 UPTO ($priority)
 Calculate the log mask for all priorities up to the given priority. More...
 
 MIN ($priority)
 Calculate the log mask for all priorities greater than or equal to the given priority. More...
 
 MAX ($priority)
 Calculate the log mask for all priorities less than or equal to the given priority. More...
 
 setMask ($mask)
 Set and return the level mask for the current Log instance. More...
 
 getMask ()
 Returns the current level mask. More...
 
 _isMasked ($priority)
 Check if the given priority is included in the current level mask. More...
 
 getPriority ()
 Returns the current default priority. More...
 
 setPriority ($priority)
 Sets the default priority to the specified value. More...
 
 attach (&$observer)
 Adds a Log_observer instance to the list of observers that are listening for messages emitted by this Log instance. More...
 
 detach ($observer)
 Removes a Log_observer instance from the list of observers. More...
 
 _announce ($event)
 Informs each registered observer instance that a new message has been logged. More...
 
 isComposite ()
 Indicates whether this is a composite class. More...
 
 setIdent ($ident)
 Sets this Log instance's identification string. More...
 
 getIdent ()
 Returns the current identification string. More...
 

Data Fields

 $_filename = 'php.log'
 
 $_fp = false
 
 $_append = true
 
 $_locking = false
 
 $_mode = 0644
 
 $_dirmode = 0755
 
 $_lineFormat = '%1$s %2$s [%3$s] %4$s'
 
 $_timeFormat = '%b %d %H:%M:%S'
 
 $_eol = "\n"
 
- Data Fields inherited from Log
 $_opened = false
 
 $_id = 0
 
 $_ident = ''
 
 $_priority = PEAR_LOG_INFO
 
 $_mask = PEAR_LOG_ALL
 
 $_listeners = array()
 
 $_formatMap
 

Detailed Description

Definition at line 20 of file file.php.

Member Function Documentation

◆ _Log_file()

Log_file::_Log_file ( )

Destructor.

Definition at line 152 of file file.php.

References close().

153  {
154  if ($this->_opened) {
155  $this->close();
156  }
157  }
close()
Closes the log file if it is open.
Definition: file.php:235
+ Here is the call graph for this function:

◆ _mkpath()

Log_file::_mkpath (   $path,
  $mode = 0700 
)

Creates the given directory path.

If the parent directories don't already exist, they will be created, too.

This implementation is inspired by Python's os.makedirs function.

Parameters
string$pathThe full directory path to create.
integer$modeThe permissions mode with which the directories will be created.
Returns
True if the full path is successfully created or already exists.

private

Definition at line 174 of file file.php.

References $path.

Referenced by open().

175  {
176  /* Separate the last pathname component from the rest of the path. */
177  $head = dirname($path);
178  $tail = basename($path);
179 
180  /* Make sure we've split the path into two complete components. */
181  if (empty($tail)) {
182  $head = dirname($path);
183  $tail = basename($path);
184  }
185 
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);
189  }
190 
191  /* Create this segment of the path. */
192  return @mkdir($head, $mode);
193  }
$path
Definition: index.php:22
_mkpath($path, $mode=0700)
Creates the given directory path.
Definition: file.php:174
+ Here is the caller graph for this function:

◆ close()

Log_file::close ( )

Closes the log file if it is open.

public

Definition at line 235 of file file.php.

Referenced by _Log_file().

236  {
237  /* If the log file is open, close it. */
238  if ($this->_opened && fclose($this->_fp)) {
239  $this->_opened = false;
240  }
241 
242  return ($this->_opened === false);
243  }
+ Here is the caller graph for this function:

◆ flush()

Log_file::flush ( )

Flushes all pending data to the file handle.

public

Since
Log 1.8.2

Definition at line 251 of file file.php.

252  {
253  if (is_resource($this->_fp)) {
254  return fflush($this->_fp);
255  }
256 
257  return false;
258  }

◆ log()

Log_file::log (   $message,
  $priority = null 
)

Logs $message to the output window.

The message is also passed along to any Log_observer instances that are observing this Log.

Parameters
mixed$messageString or object containing the message to log.
string$priorityThe priority of the message. Valid values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT, PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING, PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
Returns
boolean True on success or false on failure. public

Definition at line 272 of file file.php.

References $_eol, Log\$_priority, $success, Log\_announce(), Log\_extractMessage(), Log\_format(), Log\_isMasked(), and open().

273  {
274  /* If a priority hasn't been specified, use the default value. */
275  if ($priority === null) {
276  $priority = $this->_priority;
277  }
278 
279  /* Abort early if the priority is above the maximum logging level. */
280  if (!$this->_isMasked($priority)) {
281  return false;
282  }
283 
284  /* If the log file isn't already open, open it now. */
285  if (!$this->_opened && !$this->open()) {
286  return false;
287  }
288 
289  /* Extract the string representation of the message. */
290  $message = $this->_extractMessage($message);
291 
292  /* Build the string containing the complete log line. */
293  $line = $this->_format($this->_lineFormat,
294  strftime($this->_timeFormat),
295  $priority, $message) . $this->_eol;
296 
297  /* If locking is enabled, acquire an exclusive lock on the file. */
298  if ($this->_locking) {
299  flock($this->_fp, LOCK_EX);
300  }
301 
302  /* Write the log line to the log file. */
303  $success = (fwrite($this->_fp, $line) !== false);
304 
305  /* Unlock the file now that we're finished writing to it. */
306  if ($this->_locking) {
307  flock($this->_fp, LOCK_UN);
308  }
309 
310  /* Notify observers about this log message. */
311  $this->_announce(array('priority' => $priority, 'message' => $message));
312 
313  return $success;
314  }
_announce($event)
Informs each registered observer instance that a new message has been logged.
Definition: Log.php:811
$_eol
Definition: file.php:87
$success
Definition: Utf8Test.php:87
$_priority
Definition: Log.php:69
open()
Opens the log file for output.
Definition: file.php:204
_isMasked($priority)
Check if the given priority is included in the current level mask.
Definition: Log.php:726
_extractMessage($message)
Returns the string representation of the message data.
Definition: Log.php:417
_format($format, $timestamp, $priority, $message)
Produces a formatted log line based on a format string and a set of variables representing the curren...
Definition: Log.php:530
+ Here is the call graph for this function:

◆ Log_file()

Log_file::Log_file (   $name,
  $ident = '',
  $conf = array(),
  $level = PEAR_LOG_DEBUG 
)

Constructs a new Log_file object.

Parameters
string$nameIgnored.
string$identThe identity string.
array$confThe configuration array.
int$levelLog messages up to and including this level. public

Definition at line 98 of file file.php.

References Log\UPTO().

100  {
101  $this->_id = md5(microtime());
102  $this->_filename = $name;
103  $this->_ident = $ident;
104  $this->_mask = Log::UPTO($level);
105 
106  if (isset($conf['append'])) {
107  $this->_append = $conf['append'];
108  }
109 
110  if (isset($conf['locking'])) {
111  $this->_locking = $conf['locking'];
112  }
113 
114  if (!empty($conf['mode'])) {
115  if (is_string($conf['mode'])) {
116  $this->_mode = octdec($conf['mode']);
117  } else {
118  $this->_mode = $conf['mode'];
119  }
120  }
121 
122  if (!empty($conf['dirmode'])) {
123  if (is_string($conf['dirmode'])) {
124  $this->_dirmode = octdec($conf['dirmode']);
125  } else {
126  $this->_dirmode = $conf['dirmode'];
127  }
128  }
129 
130  if (!empty($conf['lineFormat'])) {
131  $this->_lineFormat = str_replace(array_keys($this->_formatMap),
132  array_values($this->_formatMap),
133  $conf['lineFormat']);
134  }
135 
136  if (!empty($conf['timeFormat'])) {
137  $this->_timeFormat = $conf['timeFormat'];
138  }
139 
140  if (!empty($conf['eol'])) {
141  $this->_eol = $conf['eol'];
142  } else {
143  $this->_eol = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n";
144  }
145 
146  register_shutdown_function(array(&$this, '_Log_file'));
147  }
UPTO($priority)
Calculate the log mask for all priorities up to the given priority.
Definition: Log.php:642
+ Here is the call graph for this function:

◆ open()

Log_file::open ( )

Opens the log file for output.

If the specified log file does not already exist, it will be created. By default, new log entries are appended to the end of the log file.

This is implicitly called by log(), if necessary.

public

Definition at line 204 of file file.php.

References Log\$_opened, and _mkpath().

Referenced by log().

205  {
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);
210  }
211 
212  /* Determine whether the log file needs to be created. */
213  $creating = !file_exists($this->_filename);
214 
215  /* Obtain a handle to the log file. */
216  $this->_fp = fopen($this->_filename, ($this->_append) ? 'a' : 'w');
217 
218  /* We consider the file "opened" if we have a valid file pointer. */
219  $this->_opened = ($this->_fp !== false);
220 
221  /* Attempt to set the file's permissions if we just created it. */
222  if ($creating && $this->_opened) {
223  chmod($this->_filename, $this->_mode);
224  }
225  }
226 
227  return $this->_opened;
228  }
_mkpath($path, $mode=0700)
Creates the given directory path.
Definition: file.php:174
$_opened
Definition: Log.php:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $_append

Log_file::$_append = true

Definition at line 42 of file file.php.

◆ $_dirmode

Log_file::$_dirmode = 0755

Definition at line 64 of file file.php.

◆ $_eol

Log_file::$_eol = "\n"

Definition at line 87 of file file.php.

Referenced by log().

◆ $_filename

Log_file::$_filename = 'php.log'

Definition at line 27 of file file.php.

◆ $_fp

Log_file::$_fp = false

Definition at line 34 of file file.php.

◆ $_lineFormat

Log_file::$_lineFormat = '%1$s %2$s [%3$s] %4$s'

Definition at line 71 of file file.php.

◆ $_locking

Log_file::$_locking = false

Definition at line 49 of file file.php.

◆ $_mode

Log_file::$_mode = 0644

Definition at line 56 of file file.php.

◆ $_timeFormat

Log_file::$_timeFormat = '%b %d %H:%M:%S'

Definition at line 80 of file file.php.


The documentation for this class was generated from the following file: