I have a logging script which saves to file logs.
I would like to know how I can read X amount of records from the end of the file without pulling all the data from the file.
This is an example of a record in the file:
[02.10.2011, 23:15:09] User ‘dor’ added comment.
This is the insertion code:
public function logError( string $errorMessage )
{
if( emtpy( trim( $errorMessage ) ) )
{
return false;
}
$errorMessage = trim( strip_tags( htmlspecialchars( $errorMessage ) ) );
$errorTime = date( 'd.m.Y, H:i:s' );
$errorRow = "[{$errorTime}] {$errorMessage}\n";
if( @is_writable( "{$this->logsDir}{$this->logFile}" ) )
{
if( fwrite( $this->fileHandler , $errorRow) === false )
{
return false;
}
}
return true;
}
You want to use
fseekfunction with negative offset andSEEK_ENDas whenceLook into documentation of
fseekhttp://php.net/fseek