How do i change the php ini error log?

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.

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 there is not 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 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!), 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:

  • 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 must restart your server for the changes to the php.ini file to take effect.

How do i change the php ini error log?

How to use PHP error logs effectively

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.

error_log()

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.

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:

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.

Clean up after yourself

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.

Where to go from here

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.

How do i change the php ini error log?

Start sending your logs and errors to Stackify by signing up for a free trial.

Have fun and happy coding!

  • About the Author
  • Latest Posts

How do I change PHP INI display errors?

Quickly Show All PHP Errors The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

Can I delete PHP error log?

It is entirely safe to just delete the php. log file. It will be auto-created the next time it is needed.

How do I enable PHP error logging?

Enable Error Logging in php. If you want to enable PHP error logging in individual files, add this code at the top of the PHP file. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); Now, you must enable only one statement to parse the log errors in the php.

Where can I find PHP error logs?

The location of the error log file itself can be set manually in the php. ini file. On a Windows server, in IIS, it may be something like "'error_log = C:\log_files\php_errors. log'" in Linux it may be a value of "'/var/log/php_errors.