Green Acorn

Blog

PHP Error Reporting & Logging

In PHP there are two options that control what errors are reported and whether these errors are shown to the user. Error reporting sets the error reporting level with an assigned integer, typically via a constant. By default, PHP reports everything except E_NOTICE and E_STRICT prior to PHP 5.4.

Below is a list of reporting levels:

Value Constant Description
1 E_ERROR Fatal run-time errors. Errors that can not be recovered from. Execution of the script is halted
2 E_WARNING Non-fatal run-time errors. Execution of the script is not halted
4 E_PARSE Compile-time parse errors. Parse errors should only be generated by the parser
8 E_NOTICE Run-time notices. The script found something that might be an error, but could also happen when running a script normally
16 E_CORE_ERROR Fatal errors at PHP startup. This is like an E_ERROR in the PHP core
32 E_CORE_WARNING Non-fatal errors at PHP startup. This is like an E_WARNING in the PHP core
64 E_COMPILE_ERROR Fatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine
128 E_COMPILE_WARNING Non-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine
256 E_USER_ERROR Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()
512 E_USER_WARNING Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()
1024 E_USER_NOTICE User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()
2048 E_STRICT Run-time notices. PHP suggest changes to your code to help interoperability and compatibility of the code
4096 E_RECOVERABLE_ERROR Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())
8192 E_ALL All errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0)

Display_errors determines whether to show errors to the user. It’s either 1 (on) or 0 (off). By default, display_errors is set to on. Display_errors should only be set to on for development servers and off for production servers.

Below is an example on setting these configurations at runtime.

// Set error reporting
error_reporting(E_ALL | E_STRICT);
// Display errors
ini_set('display_errors', 1);

Logging errors

There are a number of options relating to error logging. The logging options are set in php.ini but can be also overridden using ini set at runtime (same as above).

Log_errors defaults to off (0) and does not log errors. Log_errors_max_len is the length of the error messages stored in the logs measured in bytes. Set this to 0 to have an unlimited length.

You can set enable this by doing the below:

// Log errors
ini_set('log_errors', 1);
// no error log message max
ini_set('log_errors_max_len', 0);

The error_log should ideally be set to outside the Web root, such as var/log/apache2/php. However, in this case I have set it to the Web root!

Below is an example on setting these configurations at runtime.

// specify log file
ini_set('error_log', './php-error-log.txt');
Author:
Posted in: PHP, Web Development

Leave a Reply

Your email address will not be published. Required fields are marked *

     

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>