Stackify is now BMC. Read theBlog

PHP Error Log Basics

By: Gaege
  |  April 5, 2024
PHP Error Log Basics

When developing PHP applications, error logs tend to be underutilized due to their apparent complexity. The reality is that PHP error logs are extremely 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.

Setting up PHP error logging

To enable error logging for your site or application, follow these steps:

  • Locate the php.ini file on your server.
  • Look for the line containing the error_reporting entry.
  • Ensure it is not commented out, i.e. with a semicolon (;) in front of the entry.
  • Set the error_reporting entry equal to the desired level of logging (covered next). For example, you might set it to error_reporting = E_ALL.

Error reporting levels

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.

  • E_ALL—Logs all errors and warnings
  • E_ERROR—Logs fatal runtime errors
  • E_WARNING—Logs non-fatal runtime errors
  • E_NOTICE—Logs runtime notices (typical bugs in code)

As a final step in the basic configuration of PHP error logging, you’ll want to decide whether or not to display the errors on the client (browser). This is easily done:

  • Look for the line containing the display_errors entry in the php.ini file
  • Ensure there is not a semicolon (;) in front of the entry
  • Set the display_errors entry to On or Off. For example, display_errors = Off will not show errors on the client

Where are the error logs?

Once you’re all set up with logging and generating plenty of errors (for practice, of course!), you’ve got to actually look at them to determine what’s going on.

The default error log tends to differ from environment to environment, so it’s advisable to manually set the location of the file. Here’s how:

  • Look for the line containing the error_log entry in the php.ini file
  • Ensure there is not a semicolon (;) in front of the entry
  • Set the error_log entry to the desired path of the log file. For example, you might use error_log = /logs/php-errors.log.

Note this, though: you’ll have to restart your server for the changes to the php.ini file to take effect.

How to use PHP error logs effectively

There are a number of handy error logging functions built directly into the PHP engine, which should be enough to cover basic use cases. Of course, 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 of the more commonly used error logging functions are covered below, but you can find a comprehensive list (naturally) in the official PHP documentation.

error_log()

The error_log()  function allows for a string (required) to be sent to the log file. The message can also be sent 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 as well. 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.

debug_print_backtrace()

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 the following:

ob_start();
debug_print_backtrace();
error_log(ob_get_clean());

This utilizes PHP’s built-in buffer as well as the previously mentioned error_log() function to provide some insight as to the source of errors produced by an application.

Clean up after yourself

PHP error logs don’t typically clear or truncate themselves by default. As such, good practice suggests you only enable logging when needed during development and you delete log files when they’re no longer needed.

Where to go from here

Now that you’ve got all the tools for effective error logging in PHP, you can debug with confidence and eventually explore some 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!

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]