Skip Navigation

php.ini Guide: Error Handling and Logging

« Back to php.ini Guide Introduction

The error handling and logging section of the php.ini file controls how errors are handled and reported. It is important to understand how this works and how it can be used to improve code. Usually, developers will use two different settings here - one for a development server, and one on the actual server used to serve the site to the public. The idea is to use these functions to provide as much help and information as easily as possible to a developer while a site is being created, and to allow the developer to ensure errors are not shown to users of the site should they occur.

error_reporting = E_ALL & ~E_NOTICE

error_reporting defines what errors you want to be informed about. This should be set to "E_ALL" or left as the default "E_ALL & ~E_NOTICE". The difference is that the second will suppress "notice" level errors. These are not actually errors as such, but PHP letting you know about things that could be done better. I usually use "E_ALL" in development, if for no other reason than I believe that ironing out "notice" level errors makes for better code.

Usually, "E_NOTICE" errors occur when you use a variable that has not been explicitly set already. For that reason alone it is worth displaying these errors. While often (especially if you have control over your own server), "register_globals" will be set to off (so variables that have not been explicitly set are not a problem), there may be a time that the code is ported to another server or the configuration of the current server is changed, and "register_globals" may be changed, creating a potential security hole in an application.

The "E_USER_" set of errors can be quite useful for error logging. These are controlled with the "trigger_error" function, and allow you to add custom error messages to log files. For example, if you wanted to add unsuccessful login attempts to your error log, you would use trigger_error in conjunction with the appropriate error_reporting settings to tell PHP to write these events to the log file.

The value of this directive is slightly unusual. You can set reporting to any or all of a list of values. They can be separated with a pipe "|" meaning "or" or a "&" meaning "and". Preceding a value with a "~" means "not". So the default value of "E_ALL & ~E_NOTICE" means "E_ALL and not E_NOTICE" - report all errors except notice messages. "E_ERROR|E_WARNING" means "E_ERROR or E_WARNING" - report errors and warnings.

Values: E_ALL, E_ERROR, E_WARNING, E_PARSE, E_NOTICE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE. Default: E_ALL & ~E_NOTICE.

display_errors = On

Displaying errors on a live website is a bad idea, for reasons of both security and professionalism. This should always, without fail, be set to "Off" on a publicly-accessible website, if at all possible. In a local development environment, leaving it "On" makes life much easier. If in doubt, switch this to "Off", and use error logging (below) to write errors to a file rather than outputting them to the user.

Values: On (default), Off

display_startup_errors = Off

display_errors being set to "On" does not mean that errors that occur while PHP is starting up will be displayed. The "display_startup_errors" setting tells PHP to output errors that occur during startup. Unless trying to track a specific bug and it appears to be a bug in a PHP module (or similar startup-related bug), I would recommend always leaving this "Off".

Values: On, Off (default)

log_errors = Off

This, when set to "On", will write any error messages to an error log file. It's a good idea to set this to "On" and display_errors to "Off" on a live site, and to check this file every so often for errors. Setting this to "Off" and also display_errors to "Off" will mean you have no record of errors available.

Values: On, Off (default)

log_errors_max_len = 1024

Your error log could become potentially very large if a lot of errors are being reported and the length is not limited. However, I usually increase this a little. I'd personally rather have to deal with an over-large log file than potentially miss important information in an error. This is very much down to personal preference though - you might prefer to just leave it as it is - it will not affect how your site runs a great deal.

Values: Integer. Default: 1024.

ignore_repeated_errors = Off

This directive is a fairly simple one. Should PHP ignore repeated errors or not? On the one hand, the repetition doesn't help to cure the problem and does just increase the size of the error log. On the other hand, repetition does help give an idea of which bugs should be fixed first, as those that occur frequently are likely to be higher priority than those that only occur once a year. Personally, I usually leave this Off.

Values: On, Off (default)

ignore_repeated_source = Off

With the above directive, "ignore_repeated_errors", the same error can be reported if it comes from a different place (eg another file). With ignore_repeated_source set to On, those errors will be ignored. This only has any effect if ignore_repeated_errors is set to On. Personally, I always leave this Off. The causes of errors can often be different and that alone is enough reason for me.

Values: On, Off (default)

report_memleaks = On

report_memleaks is one of the few directives in the php.ini file that I've never had reason to change. Setting this to "Off" will prevent memory leak errors being displayed. However, memory leaks are only displayed when you compile PHP with "--enable-debug" (which allows you to perform some advanced tasks (eg backtraces). This would never affect a production environment, and rarely a development one.

Values: On (default), Off

track_errors = Off

This is a vastly underused feature of PHP, especially when it comes to things like file handling and complicated database interaction. All it does, simply, is record the latest error in the $php_errormsg variable, allowing you to access it through PHP. I usually turn this on, simply to allow myself access to the variable $php_errormsg later.

Values: On, Off (default)

;html_errors = Off

As you would expect, this option gives you the ability to control whether or not HTML is included in error messages output by PHP. More often than not, this is going to be irrelevant - a production environment should not be outputting error messages anyway. If you are just starting out with PHP, it is probably worth setting this to "On". If you also download the PHP Manual, you will then be able to click links within error messages that go to pages explaining errors in more detail.

Values: On, Off (default)

;docref_root = "/phpmanual/"
;docref_ext = .html

These two directives relate to the "html_errors" one above. If you wish to make use of the links in error messages added because "html_errors" is "On", then you will need a copy of the PHP Manual. The directives above allow you to set where the manual can be found ("docref_root" - always include the leading slash) and what file extension is used by the manual ("docref_ext" - ".html" by default).

Values:
    docref_root: Quoted string. Default: "/phpmanual/".
    docref_ext: String. Default: .html.

;error_prepend_string = "<font color=ff0000>"
;error_append_string = "</font>"

If you wish to format error messages, this and the following instruction are the best way to do so. Any HTML you wish to place before an error message should be placed above. As I try to code clean HTML, I usually swap the above for something like "<span style='color:#f00;'>" and "</span>" but you can add almost any HTML here that suits you.

Values: Both elements can take any quoted HTML. Please note that there is no escape character for this value - you cannot use quotation marks in your HTML (unfortunately).

;error_log = filename

If you want to direct errors to a specific log file, you can use this directive to specify which log file should be used. The filename should be a full file path. I usually use this to control where PHP errors are written to.

Values: File Path.

;error_log = syslog

This won't work on Windows 95. This settings just needs to be uncommented to work, and instructs PHP to report all errors the standard Windows log system. I leave this off.

Values: syslog (default). No alternative values.

5 comments

Where is the log file stored by default? Is it possible to change the path of this file putting it outside the php instalation path?
There are several log files. If you mean the log file specified in ";error_log = filename", there is no default. If not specified, and you turn on "log_errors" without specifying a log file in "error_log", then PHP will log errors to the Apache error log.

"error_log" itself can be set to anywhere, including outside the PHP installation path.
I run PHP on a Windows 2003 Server at work. Do you know if there's a way to make PHP report to a different system log other than the default "Application" event log? I'd love to move all of my PHP errors to their own log. For now though, I'm just using a file to trap the errors.
DrTebi
United States #4: November 16, 2006
Thanks for this post, it's good to have a nice overview of the error logging possibilities.

You should add one note there though:
the "error_log" directive logs to a specific file, and this file MUST be writable by the web server. Otherwise PHP only logs startup errors.

DrTebi
premg
Unknown #5: October 13, 2007
I would to know,how the error "error-log switched off " happening while using apache?

Post Your Comment

· Comments with keywords instead of a name have their URLs removed.
· Your email address will not be displayed or shared.

Live Comment Preview

 United States #6: 1 minute ago