Function to write a message to a text file

Sometimes , we need to write some logging information to a text file in php. The following function takes  filename and message as the parameter and logs whatever you pass to the file with date and time.

function logToFile($fileName, $logMessage)
{
    $logDirectory = “/path/to/log/dir”;     
    $fileHandler = fopen($logDir . '/' . $fileName , "a");
    $logText = date("Y-m-d h:i:s") . "---" . $logMessage . "\r\n";
    fwrite($fileHandler, $logText);
    fclose($fileHandler);
}