7

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?

chattsm
  • 4,651
  • 5
  • 19
  • 20
  • Unless I'm missing something, it should be logging notices already. Have you checked whether it's using some custom error handler? – Álvaro González Aug 22 '11 at 15:47
  • @Álvaro G. Vicario - my thoughts exactly. There is no custom error handler, when I turn on displaying of errors (display_errors = On), notices are displayed, but not logged! – chattsm Aug 22 '11 at 16:01
  • Just to start narrowing the issue, you could forget the actual code base and try [this simple snippet](http://pastebin.com/6NCRcaUd). – Álvaro González Aug 22 '11 at 16:08
  • See this: http://il2.php.net/manual/en/function.set-error-handler.php – Michael Aug 22 '11 at 15:44
  • @Álvaro G. Vicario - that snippet doesn't log anything at all, no file 'foo.log' is created. – chattsm Aug 22 '11 at 16:29

3 Answers3

3

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");
AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
  • A thorough and well constructed answer. However, turning on display_errors does show notices, they are just not logged, and the server is running PHP 5.3.5, so the E_DEPRECATED issue is not applicable. I'd like to avoid writing my own error handler, I just can't work out why notices will display, but won't log! – chattsm Aug 23 '11 at 09:12
  • @m.p.c I didn't realize these errors were displaying in the browser. It seems like your `error_reporting` levels are fine, and its the log file itself that's having an issue. I've made an update at the top of my answer. – AgentConundrum Aug 23 '11 at 17:56
2

Turns out the system has a custom Apache ErrorLog directive defined, and I have been tail -f ... the default Apache error_log.

Note to self: always check the web server setup first before posting silly questions on SO!

chattsm
  • 4,651
  • 5
  • 19
  • 20
0

You can create your own handler

<?php


function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if ($errno == E_USER_NOTICE){
        /*  log actions here */
    }
}

set_error_handler("myErrorHandler");
genesis
  • 50,477
  • 20
  • 96
  • 125
  • I'd like to avoid adding a custom error handler, this doesn't solve the problem of notices displaying when I turn on display_errors = On, but still not logging. – chattsm Aug 22 '11 at 16:04