self::MAX_SIZE) { self::rotateLog(); } $date = date('Y-m-d H:i:s'); $message = sprintf("[%s] [IP:%s] [REASON:%s]" . PHP_EOL, $date, $ip, $reason); // 2. Append to log file file_put_contents(self::LOG_FILE, $message, FILE_APPEND | LOCK_EX); } /** * Rotates the log: * 1. Deletes the .old file * 2. Renames current .log to .old * 3. Current logging continues in new empty file */ private static function rotateLog() { $backup_file = self::LOG_FILE . '.old'; // Remove ancient backup if (file_exists($backup_file)) { @unlink($backup_file); } // Rename current to backup @rename(self::LOG_FILE, $backup_file); } }