In every project we get a need to create log files. We can easily debug our code in production by using log files. So always try to put log files in each operation in your project.
Here I am posting a small script which you can use in any of your file or in constructor of class or in common config or function file to start creating log file.
Add Log File In Temp (It should be at the starting of your code like: config file or common function file or if you want to add it for one page only then include the lines it in that page)
//start creating log file $uploadDir = sys_get_temp_dir(); $now = new DateTime; $now = $now->format('d-m-Y'); $filename = $uploadDir.DIRECTORY_SEPARATOR.$now.time().'__myprojectlog.txt'; $fileResource = fopen($filename, "a") or die("Could not open log file.");
Now a log file will be created in tmp directory. In unix it is in the root of your server like /tmp.
Add Success/Error Logs in file
Now its time to add logs in your log file. Here is a small example to achieve this:
<?php $filePath='/var/www/test/img.jpg'; $fileErrorLog = "Error while uploading file:".$filePath; fwrite($fileResource, date("d-m-Y, H:i:s")." - $fileErrorLog \n"); ?>
That’s all. Now Check your temp directory for error log file. 🙂