Lots of beginners and also developers with few years of experience are doing the same errors and that’s they don’t know what’s going on in their application. When you have PHP without debug the error you will get will not help you much. There are lots of information missing like stack trace, environment, etc. Also you will not have this information when deployed to production. There are many tools for that, mostly part of the framework, but if you are not using any, it might be challenging to do it by yourself. These tools, when configured can tell you more even about SQL queries you executed, supports different tools like Redis, RabbitMQ. So lets have a look at how to install them to our vanilla code without framework.
One of the easy to install and yet powerfull is Tracy (originally part of Nette framework).
Installation
First you need to install the library using Composer. If you are not using composer, you should.
composer require tracy/tracy
Usage
Then you will just add short snippet to the first file which is shared between all the pages or you can add it into the preload in php.ini. So let’s assume that you have config.php which is included in every page. We will add these lines at the beginning.
use Tracy\Debugger;
Debugger::enable(Debugger::DETECT, __DIR__ . '/log');
The first parameter will detect whether this is development server or live server. This is done using clients IP address. If it’s localhost, then debug mode will be enabled. If you are running your application in docker or different machine, this will not work. So you can put Debugger::DEVELOPMENT
there. But do not deploy that to production server. Second parameter is path to log folder. All errors, notices, etc. will be stored then on production server. You can also setup SMTP so you will be notified about every new error.
Debugger::$email = 'admin@example.com';
When developing, you will know about every notice, that goes even for AJAX calls.
You will not dependent on configuration of the server. If anything will go wrong, you can always look into the log folder and see if there was an error for your call. There are possibilities to link Doctrine and other libraries.
For more see the documentation: https://tracy.nette.org/en/guide