Search code examples
phploggingexceptionerror-handlingerror-logging

Send PHP errors to default error log plus user-defined error log


I may just be having trouble with the error-handling documentation, but what I have in mind is a few different scenarios:

  1. When PHP throws a typical error, I want to keep the default error logging i place but also have separate error logs based on the error type / severity (a log for only fatal errors so I don't have to scan past millions of minor errors, but still have those minor errors logged in the general pile).

  2. For a webapp / platform (like Wordpress or MediaWiki, etc), have PHP errors (all, not just those thrown by webapp) stored in the web app error log. (This will almost certainly be severity based. The idea is to go to the webapp error log and being able to see PHP fatal errors mixed in to avoid hunting down that domain's error log, etc)

  3. Also having errors of certain types thrown by the webapp go to the general error log and it's own custom log (I know I've seen one or the other, but not both).

So the root question is: How can I have a custom error log without losing/overriding the default error log?

Also, is it pretty safe to assume that in most cases errors and exceptions at this level are interchangeable, or will I need to take other steps for similar exception handling goals?


Solution

  • You can set your own error handler and still let php handle the triggered error as usual

    http://se.php.net/manual/en/function.set-error-handler.php

    set_error_handler(function($errno, $errstr, $errfile, $errline) {
      // do what you want  
      return false; // returning false makes PHP execute its own error handler as well
    });