EventLog: ezcLogFileWriter
[ ]
[ ]
[ ]
[ ]
[ ]
Class: ezcLogFileWriter
|
The ezcLogFileWriter class provides functionality to write log files to the file system. [
source]
Implemented Interfaces
The main purpose is to keep track of the various log files and support log rotation, although log rotation can also be disabled. The file format of the log should be implemented in a subclass.
The following example implements a new log writer that writes the output in (
http://www.php.net/print_r format) to a file:
1. class MyLogWriter extends ezcLogFileWriter
2. {
3. // Call parent constructor. (In this case, it possible to omit the constructor.)
4. public function __construct($dir, $file = null, $maxSize = 204800, $maxFiles = 3 )
5. {
6. parent::__construct($dir, $file, $maxSize, $maxFiles );
7. }
8.
9. // Implement the ezcLogWriter interface:
10. public function writeLogMessage( $message, $type, $source, $category, $extraInfo = array() )
11. {
12. // Create a message
13. $res = print_r( array( "message" => $message, "type" => $type, "source" => $source, "category" => $category ), true );
14.
15. // And call the parent class
16. $this->write( $type, $source, $category, $res );
17. }
18. }
Descendents
| Child Class |
Description |
| ezcLogUnixFileWriter |
Writes the log messages to a file in a format that is frequently used on the Unix operating system.
|
Member Variables
|
protected ezcLogFilterSet |
$fileMap
Keeps track of which group of messages should be stored in what file. |
|
protected string |
$logDirectory
Directory where the log files should be placed. |
|
protected int |
$maxFiles
Maximum log rotation files with the same name.
When rotating and the max limit is reached, the oldest log is discarded. |
|
protected int |
$maxSize
Maximum file size before rotation, or false when log rotation is disabled. |
|
protected array(resource) |
$openFiles
= array()
Contains all the open files. The first file in the array is always the default file. |
Method Summary
|
public ezcLogFileWriter |
__construct(
$logDirectory, [$defaultFile = null], [$maxLogRotationSize = 204800], [$maxLogFiles = 3] )
Constructs an ezcLogFileWriter. |
|
public void |
__destruct(
)
Destructs the object and closes all open file handles. |
|
protected resource |
openFile(
$fileName )
Returns the filehandle of the $fileName. |
|
protected bool |
rotateLog(
$fileName )
Rotates a log and returns true upon success. |
|
public void |
setFile(
$logFilter, $fileName )
Maps the filename $fileName to the messages specified by the ezcLogFilter $logFilter. |
|
protected void |
write(
$eventType, $eventSource, $eventCategory, $string )
This method writes the $string to a file. |
Methods
__construct
ezcLogFileWriter __construct(
string
$logDirectory, [string
$defaultFile = null], [int
$maxLogRotationSize = 204800], [int
$maxLogFiles = 3] )
Constructs an ezcLogFileWriter.
The log files will be placed in the directory $logDirectory.
If the file $defaultFile is not null, log messages that are not map() to any file are written to this $defaultFile. If $defaultFile is null, then log messages are discarded.
Set $maxLogRotationSize to specify the maximum size of a logfile. When the maximum size is reached, the log will be rotated. $maxLogFiles sets the maximum number of rotated log files. The oldest rotated log will be removed when the $maxLogFiles exceeds. Log rotation can be disabled by setting $maxLogRotationSize to false.
Parameters
| Name |
Type |
Description |
$logDirectory |
string |
|
$defaultFile |
string |
|
$maxLogRotationSize |
int |
|
$maxLogFiles |
int |
|
__destruct
void __destruct(
)
Destructs the object and closes all open file handles.
openFile
resource openFile(
string
$fileName )
Returns the filehandle of the $fileName.
If the maximum file size is exceeded, the file will be rotated before opening.
Parameters
| Name |
Type |
Description |
$fileName |
string |
|
Throws
| Class | Description |
ezcBaseFilePermissionException |
if the file can't be opened, created, or when the directory is not writable. |
rotateLog
bool rotateLog(
string
$fileName )
Rotates a log and returns true upon success.
Parameters
| Name |
Type |
Description |
$fileName |
string |
|
setFile
Maps the filename $fileName to the messages specified by the
ezcLogFilter $logFilter.
Log messages that matches with the filter are written to the file $fileName.
Parameters
| Name |
Type |
Description |
$logFilter |
ezcLogFilter |
|
$fileName |
string |
|
write
void write(
int
$eventType, string
$eventSource, string
$eventCategory, string
$string )
This method writes the $string to a file.
The file to which the string will be written depends on the $eventType, $eventSource, and $eventCategory.
Parameters
| Name |
Type |
Description |
$eventType |
int |
|
$eventSource |
string |
|
$eventCategory |
string |
|
$string |
string |
|
Throws
| Class | Description |
ezcLogWriterException |
if it was not possible to write to the log file. |
Last updated: Tue, 01 Sep 2009