3

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?

4
  • I'd say: Ask that your sysadmin who should be able to script up something in perl/linux wihtin minutes that does what you told her/him to do. Because by default, PHP does not have the feature you're looking for. So you need to add it. Commented May 5, 2012 at 13:28
  • @hakre - one of the goals is portability, with the web app writing to it's own plus default. The other limit to your suggestion (which isn't a bad one at all), is that I know more than my sysadmin but don't have their access, so this is an attempt to lower dependence on them. Commented May 5, 2012 at 13:31
  • 1
    You can write your own error handler per application, standard logging is still active but you can write your own log as well. Might even work for fatal errors (but I'm not that sure about that one). Commented May 5, 2012 at 13:37
  • @hakre - so writing my own doesn't disable the default? I guess that's what my question really comes down to. That and how to send errors/exceptions from my app to the default + custom. Commented May 5, 2012 at 13:38

2 Answers 2

4

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
});
Sign up to request clarification or add additional context in comments.

2 Comments

return false is the trick. I'd have never guessed. I'm going to read up on exception handling to see if I need more info on those. Thanks!
thanks for that, I've been running my own error handler code so I can email myself some errors like when a payment gateway call fails or returns 404, but doing this prevented the system logs being filled out too - this gives me the best of both worlds.
0

You can use custom error handler and log message into different files like that,

    function myErrorHandler($errno, $errstr, $errfile, $errline)
    {
        if (!(error_reporting() & $errno)) {
            return false;
        }
        $logFile = "";
        switch ($errno) {
        case E_USER_ERROR:
            $logFile = "logs/php-errors-".date("yyyy-mm-dd",time());
            exit(1);
            break;

        case E_USER_WARNING:
        $logFile = "logs/php-warnings-".date("yyyy-mm-dd",time());
            break;

        case E_USER_NOTICE:
        $logFile = "logs/php-notices-".date("yyyy-mm-dd",time());
            break;

        default:
        $logFile = "logs/php-unkown-".date("yyyy-mm-dd",time());
            break;
        }

      error_log($errstr,3,$logFile);
        return true;
    }

and you should set this error handler function using

    set_error_handler("myErrorHandler");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.