How can including a separate file for tracking user activity improve the efficiency of the logging function in PHP?

When logging user activity in PHP, including a separate file for tracking user activity can improve efficiency by offloading the logging process to a separate file. This separation allows the main PHP script to continue executing without being slowed down by the logging operations. Additionally, having a dedicated file for logging can make it easier to manage and analyze user activity data.

// Separate file for tracking user activity
// activity_logger.php

function logUserActivity($user, $action) {
    $logMessage = "[" . date('Y-m-d H:i:s') . "] User " . $user . " performed action: " . $action . PHP_EOL;
    
    // Append log message to a log file
    file_put_contents('user_activity.log', $logMessage, FILE_APPEND);
}