Knowledge Base
MilesWeb / Web Hosting FAQ

Steps to Enable PHP Error Logging & Automation

Approx. read time : 6 min

If you are a good developer, you might be always having a keen attention on the potential errors while developing a web application. You might be setting up workflows in a way to help you log errors properly. With PHP, you get some of the most robust method to manage the PHP error logs which can be logged manually and then automated. You will also find some third party tools that are developed by the open source community for handling the PHP error logging process.

What you should think upon is that when the errors should be logged and how that should be done? While working in the dev mode, you can log PHP errors as per your preference. You can create a PHP error log file or save them as notifications on different channels. You can always log errors conveniently as per your requirements.

Steps to Automate PHP Error Logging

For now, when you are working in a production mode, you should create a smooth workflow to log errors in the backend, so that the users don’t face any glitch during runtime execution.

In this article, you will get some handy PHP error logging tips which can be helpful for potential developers.

Getting Started with PHP Error Logging

PHP is the most popular and widely used programming language for developing web applications. As per the builtwith insights, today almost 37,805,937 websites use PHP as their backend language, which makes it around 75% of all the websites in the world. These stats clearly display that PHP still has a highest market share in the programming world.

While developing an application in PHP, you use few commands like print_r(),var_dump() to debug errors and log on browser. But while working in the production mode, that is not the safest way. In dev mode, you can do it, but you need to disable it when initiating the migration. Therefore, it is easy to log errors in PHP with error_log() function in dev mode, which sends an error message to the defined error handling routines.

Let’s imagine that you are connecting MySQL database with PHP and if it fails to connect with it, you can log errors in PHP as below:

Enabling Error Logging in php.ini

In order to log errors in PHP, open the php.ini file and uncomment/add the below lines of code:

For enabling the error logging in individual files, add this code at the top of the PHP file.

Now, you should enable only one statement to parse the log errors in php.ini file:

With this you can easily see logged errors in your browser. Some of the additional commands you can write for PHP error logging include:

Copying PHP Error Logs to File

The above mentioned practices work well when you work in the dev environment. But, while taking your website live and starting your work in the production mode, it is essential to hide the errors from on screen display and log them within a backend file. These can be saved at a specific PHP error logs location whose URL is already described in the php.ini file.

PHP stores error logs in /var/log/apache2, if it uses an apache2 module. Shared hosts have a tendency to store PHP error log files in your root directory /log subfolder. But, if you have access to the php.ini file you can do this as below:

In case you are using cPanel, the master log file, the thing you’re looking for is stored (by default) at:

If everything fails, you can check the PHP error logs location with the below code:

Error Logging in PHP Frameworks

The above steps are for core PHP development. PHP has several Model View Architecture (MVC) and micro-frameworks which have their own methods on top of the core functions. Some of the best PHP MVC frameworks to build advanced web applications with comprise of: Laravel, CodeIgniter and Symfony.

Error Logging In Laravel

After setting up a new Laravel project, error logging and exception handling come pre-configured in it. Laravel has a separate class App\Exceptions\Handler that handles all these issues. For logging Laravel errors, it uses the Monolog library that supports array of log handlers. Several of these handlers are configured by Laravel for you, letting you to choose between a single PHP error log file, rotating log files and writing error information to the system log.

You can configure the PHP debug option in config/app.php file to log the errors on the display of user. This option’s value can be set in .env file under property APP_DEBUG which is an environment variable, thus setting up in app.php file. When the development is done locally, the value can be set to true, and after migrating to production, it should be set as false. Otherwise, the security concern always remains there, as it shows error on browser screen.

Being the developer, you can save Laravel log information on a single file, daily file, syslog and the errorlog. For configuring these options for Laravel error logging, open app.php file and edit the log option. For example, if you want to setup daily logging for errors, follow the below code:

Monolog is enable to log errors with different security warnings. It adds all the errors in storage by default, but you can identify them as error, alert, emergency, critical and warning. For this, add ‘log_level’ property in options as below:

You will get the log file in storage/logs. Log facades can also be used to log errors.

Symfony comprises of a default logger that can be injected into controller showing different warning levels. Log entries are written in the var/log/dev.log file by default when you’re in the dev environment. Logs are written in the var/log/prod.log in the production environment.

 

Symfony being the parent framework for Laravel, the warning levels of the framework will be the same.

Monolog is used by Symfony for error logging and Symfony pre-configures some basic handlers in the default monolog.yaml. Check the below example that uses syslogs to write logs on the file:

Automating PHP Error Logging Process

The complete work of error logging can be done manually, but while working in multiple teams and larger projects, you must setup an automated workflow which can log errors in third party services like Slack, Sentry, Blackfire etc.

With these tools you can get a better understanding of errors and their solutions. It is possible to setup Slack channels to send quick notifications to the teams about any runtime incidents.

Suppose that you have already setup Slack channel and webhooks as below:

In the above example, you will also find the emojis setup? This indicates the ways you can send Slack notifications to any particular channel as per your needs.

Bottom Line

This article demonstrates the ways to log errors in PHP by configuring the php.ini file. Since error handling is one of the critical tasks of website developers, and knowing the right way to log errors, in both modes– dev and production – is a serious job for them. This article displays an easy process for enabling and automating PHP error logging, without disturbing the dev.

If you find this article helpful, and interested to share your views about the topic, write down your suggestions in the comments section below.

Avatar
Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.
Need help? We’re always here for you.