Logging PHP notice errors

chattsm picture chattsm · Aug 22, 2011 · Viewed 8.4k times · Source

I have recently taken over development of a legacy system and want to be able to turn on logging of PHP E_NOTICE's on the deployment environment. The deployment environment ini has the following directives...

error_reporting = E_ALL & ~E_DEPRECATED
display_errors = Off
log_errors = On

I have compared the error_reporting bitmask by using echo (E_ALL & ~E_DEPRECATED).' = '.error_reporting();, and both match, so I know the error_reporting level isn't changed within the system itself, and if I turn display_errors = On notices are displayed, but not logged.

So how can I start logging PHP E_NOTICE's?

Answer

AgentConundrum picture AgentConundrum · Aug 22, 2011

Update:

According to @m.p.c in the comments on this answer, errors are being displayed in the browser when display_errors is on, but they aren't appearing in the log. I was assuming errors weren't appearing at all.

Are E_NOTICE errors the only ones that aren't appearing in the log, or are all error types affected? If it's all error types, then the first thing I would check is whether or not error logging is even enabled. Try setting ini_set('log_errors', 'On'); at the top of your script. If that doesn't work, then try setting your log file to something you're sure your server can write to by calling ini_set('error_log', 'your_file_path');. If neither of these work, then I think something is seriously wrong with your PHP install. If either of these fixes work, you can put them into your actual php.ini if you have access.


Original Answer:

Based on the error_reporting level in your question, your PHP install should already be setup to report E_NOTICE errors. If it's not logging these errors, something is wrong. I would suggest turning on display_errors to see if any E_NOTICE errors are displayed. If you can't change the php.ini file, try running ini_set('display_errors', 'On'); at the top of your script. Obviously, these errors will only show up and/or be logged if you trigger one, so you should double check that you're actually doing that somewhere.

One caveat is that the E_DEPRECATED error level was only introduced with PHP 5.3. When I just tested setting E_DEPRECATED on a PHP 5.2 install, PHP responded with errors, and set the error_reporting level to 0, which means it reports no errors at all. While it makes no sense for a pre-5.3 php.ini file to use this setting, I feel it's important to at least raise the possibility that you're using E_DEPRECATED on a server that doesn't support it. If you're not sure of your version, you can run phpinfo() and it will display a page with lots and lots of information, including the version number for your install.

Notwithstanding the above, if I've misunderstood your question and you're only talking about creating your own custom logging, then you need to create a function to run when an error occurs and assign it as the error handler using the set_error_handler() function.

It's important to note that when you use set_error_handler(), you bypass PHP's default error handler entirely. This means that your error_handler level becomes meaningless. All errors, regardless of their severity, will be passed to the error handler function you've created. The first parameter passed to this function by PHP will be the error number, which is the numeric value of the E_xxx constant of the error that was found. You'll need to write custom code to catch only the errors you want.

For example, to catch only E_NOTICE errors, your function would look like this:

function my_error_handler($errno, $errstr, $errfile, $errline) {
  if ($errno == E_NOTICE) {
    // handle/log the error
  } 
}

set_error_handler("my_error_handler");