How to Log Errors and Warnings into a File in a PHP

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

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

When I started work with Button template, Generic template(these are the Facebook chatbot template) our app was not responding back to the users. And as all process operates in the background I got stuck on the problem for almost 2 hours.

Then suddenly I got an idea of logging the errors in a file so I can track what’s wrong with the code. And then I was able to track code 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 error.log file in your root directory.

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('log_errors', true);
ini_set('error_log', 'errors.log');
ini_set('log_errors_max_len', 1024);

Once above code added, all your application errors and warnings start logging into the error.log file. This tip is helpful for live server also to track the problems if any in your code. Just make sure you set ‘display_errors’ as false on production server. As here true means display errors on the website itself which of course we don’t want to.

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 *