When developing PHP applications, error logs are under-used because of their apparent complexity. PHP error logs are helpful, especially when configured and used properly.
While there are advanced tricks to truly squeeze every last drop of utility out of error logs, this article will cover the basics of configuration and the most common use cases so you can get up and running quickly.
Once you’ve gotten comfortable using error logs effectively, you can move on to more advanced tools for further enhancing your PHP development productivity.
To enable error logging for your site or application, follow these steps:
There are numerous reporting levels to allow you to select exactly what you’d like to be notified of. Below are some of the most commonly used ones. A full list can be found in the official PHP documentation.
As a final step in the basic configuration of PHP error logging, you’ll want to decide whether to display the errors on the client (browser). This is easily done:
Once you’re all set up with logging and generating plenty of errors (for practice!), you’ve got to actually look at them to determine what’s going on.
The default error log differs from environment to environment, so it’s advisable to manually set the location of the file. Here’s how:
Note this, though: you must restart your server for the changes to the php.ini file to take effect.
There are several handy error logging functions built directly into the PHP engine, which should be enough to cover basic use cases. When your codebase gets to a point where it needs more advanced solutions, something like Stackify’s post on PHP logging best practices may be what you’re looking for
Some more commonly used error logging functions are covered below, but you can find a comprehensive list (naturally) in the official PHP documentation.
The error_log() function allows for a string (required) to be sent to the log file. You can also send the message to an email address. There are a few other options, but they’re more advanced so they won’t be covered here.
In its most basic form, error_log() writes a message to the error log:
error_log(“There’s been a problem!”);
Using this function to troubleshoot data structures or variables is fairly straightforward. Here’s an example:
$toolsArray = array("Retrace", "Basic Error Logging", "Hammer"); error_log(print_r($toolsArray, true));
In this way, the contents of the variable sent as the first parameter of the print_r() function will be displayed in the logs, which is a great way to keep track of data that may change over time or needs to be monitored during development.
While viewing code backtraces isn’t typically done via the error log files, it can be helpful for smaller applications or when getting familiar with troubleshooting.
The most readable way to send a backtrace to the log file is:
ob_start(); debug_print_backtrace(); error_log(ob_get_clean());
This uses PHP’s built-in buffer as well as the previously mentioned error_log() function to provide insight as to the source of errors produced by an application.
PHP error logs don’t typically clear or truncate themselves by default. Good practice suggests you only enable logging when needed during development and you delete log files when they’re no longer needed.
Now you’ve got all the tools for effective error logging in PHP, you can debug with confidence and eventually explore more advanced utilities like Retrace. Retrace allows you to see the error in context with the rest of your logging messages. These additional insights will give you a trail into what was happening leading up to the error.
Start sending your logs and errors to Stackify by signing up for a free trial.
Have fun and happy coding!