What is the best way to implement a logging function in PHP for tracking user activity on a portal?

To implement a logging function in PHP for tracking user activity on a portal, you can create a function that writes relevant information to a log file whenever a user performs an action. This can include logging details such as the user's IP address, the action taken, and the timestamp. By including this logging function in key areas of your portal, you can easily track user activity and troubleshoot any issues that may arise.

function logUserActivity($userId, $action) {
    $logFile = 'user_activity.log';
    $timestamp = date('Y-m-d H:i:s');
    $ipAddress = $_SERVER['REMOTE_ADDR'];
    
    $logMessage = "$timestamp - User $userId from IP $ipAddress performed action: $action\n";
    
    file_put_contents($logFile, $logMessage, FILE_APPEND);
}

// Example usage:
logUserActivity(123, 'Logged in');