What are alternative methods to constantly monitor a log file in PHP without causing high CPU usage?

Constantly monitoring a log file in PHP can lead to high CPU usage if done inefficiently. One alternative method to avoid this is to use a combination of file modification time checks and sleep intervals to periodically check the log file for updates. By only checking the file when necessary and adding a small delay between checks, you can reduce CPU usage while still effectively monitoring the log file.

$logFile = 'path/to/logfile.log';
$lastModifiedTime = 0;

while (true) {
    clearstatcache();
    $currentModifiedTime = filemtime($logFile);

    if ($currentModifiedTime > $lastModifiedTime) {
        // Log file has been updated, do something with the new content
        $lastModifiedTime = $currentModifiedTime;
    }

    sleep(1); // Adjust the sleep interval as needed
}