The $log API variable enables you to create, manage and filter log entries.

The $log API variable requires ProcessWire 2.5.3 or newer. It can be accessed via the following calls:

$log = $log; // in template context
$log = wire('log'); // in any context
$log = $this->wire('log'); // in template or Wire-derived class context
$log = $this->log(); // in template or Wire-derived class context

All log files live in ProcessWire's logs directory at: /site/assets/logs/. Log files are not web accessible. To view log files, you must get them directly from the server's file system. In ProcessWire 2.5.15 or newer, you can use the included ProcessLogger module to view them (accessible in your admin at Setup > Logs).

$log functions

Please note that other than the save(), message() and error() functions below, all functions listed require ProcessWire 2.5.15 or newer.

$log->save('name', 'text')Save the 'text' to an entry in a a log file named 'name'. If log does not exist, it will be created.
$log->message('text')Save 'text' as an entry to the 'messages' log file.
$log->error('text')Save 'text' as an entry to the 'errors' log file.
$log->getLogs()Return an array of all log files with 'name', 'file', 'size' and 'modified' properties for each.
$log->getFilename('name')Return the filename associated with the log named 'name'.
$log->getLines('name')Get an array of the most recent 100 lines added to the log file (sorted newest to oldest).
$log->getLines('name', $options)Same as above, but with an array of $options. See details in the options section below this table.
$log->getEntries('name')Get an array of the most recent 100 entries added to the log file (sorted newest to oldest). This is similar to getLines() except that each entry is an array containing 'date', 'user', 'url' and 'text' properties (all strings).
$log->getEntries('name', $options); Same as above, but with an array of $options. See details in the options section below this table.
$log->getTotalEntries('name')Returns an integer with the total number of entries present in the requested log.
$log->delete('name')Delete the log file.
$log->prune('name', 30)Prune the log file to contain only entries from the last 30 days. Replace "30" with any number of days.

The $log->getEntries() and $log->getLines() methods mentioned in the table above accept various options as the second argument:

  • limit (integer): Specify number of lines.
  • text (string): Text to find.
  • dateFrom (int|string): Oldest date to match entries.
  • dateTo (int|string): Newest date to match entries.
  • reverse (bool): Reverse order (default=true)
  • pageNum (int): Pagination number 1 or above (default=0 which means auto-detect).

Class-specific logs

As of ProcessWire 2.5.15 or newer, every Wire derived class can save a log entry to its own dedicated log file simply by calling the $this->log('text'); method. If your class was named HelloWorld, then it would save to a log file called /site/assets/logs/hello-world.txt:

$this->log("Text to save to log file"); 

See also: Blogging about Logging

Comments

No comments yet. Be the first to post!

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.