How to Log Errors and Warnings(into error.log file) in a PHP

This is a quick post on error logging in PHP. The reason for sharing this small tip is I found it can be helpful for users to track the errors or debug the errors which are thrown in the background.

Recently, I was working on integrating Facebook chatbot for one of our clients. While developing a Facebook chatbot we need to set up a webhook to which Facebook sends a user’s message in the background. Then our code should respond back on the basis of the message.

For some reason, our app was not responding back to the users. This whole process operates in the background and I got stuck on the problem for almost 2 hours.

Then suddenly I got the idea of logging the errors in a file so I can track what is wrong with the code. And then I was able to track errors and resolved all the problems. Without error logs, I couldn’t solve my problem as I was in a completely wrong direction.

Place the below code in your configuration file which will create an error.log file in your root directory.

<?php
ini_set('log_errors', true);
ini_set('error_log', 'error.log');

Once the above code is added, all your application errors and warnings start logging into the error.log file. This tip is also helpful for live servers to track the problems in your code.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *