Create a PHP file log

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");

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

Filed in: PHP

Share this post