Create a PHP file log

 In PHP

Writing a file log on disk with php it’s pretty straigt forward. Here is a simple php class that can help you log to a file. The class is logging on the directory where the script exists. You can also create a directory where the script inside and change the path in order to log to the directory you created. In the constructor of the class you should pass the name of the log file. Every time you call the function WriteLogToFile it appends messages in the file log of the directory script.


class FileLogger{
private $filename;
public function __construct($file) {
$this->filename = $file;
}

public function WriteLogToFile($messageLog) {
$currentDir = dirname(__FILE__)."/";
$fileLog = fopen($currentDir.$this->filename, 'a+');
fwrite($fileLog, $messageLog."\r\n");
fclose($fileLog);
}
}
?>

Calling function example:
$filel = new FileLogger('file.log');
$filel->WriteLogToFile('log...');

Recent Posts
Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search