5

I have configured the error_log directive in my php.ini file, like this:

error_log = /path/to/logs/error_log

And then I configured the error_reporting directive like this:

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

When I check the error_log file, I see normal PHP warning/error text lines:

[03-Jun-2015 08:39:00 America/Bogota] PHP Notice:  Undefined index: cerrar in /fake/path/to/file2.php on line 68
[03-Jun-2015 08:40:49 America/Bogota] PHP Notice:  Undefined index:  in /fake/path/to/file2.php on line 344

Is there is a way to change output format? I mean, if I can print, for example, the IP address and the subdomain that cause the warning.

I was looking for it on Stack Overflow, in Google Search, and I don't find clear information or examples.

5
  • 3
    I think then you would have to write your own error log function and simply implement it into your custom error handler Commented Jun 3, 2015 at 20:54
  • 1
    you'd have to change that in the PHP source, and compile your own custom php. you can only control WHICH messages/warnings/errors get logged, not their text format/content. And in general, PHP has no knowledge of what IP/hostname was used to invoke the script. that information can be found from $_SERVER and whatnot, but PHP itself has no knowledge of what mappings in the webserver caused it to get invoked. Commented Jun 3, 2015 at 20:55
  • 2
    To go along with @Rizier123, check php.net/manual/en/function.set-error-handler.php Commented Jun 3, 2015 at 20:58
  • Initially, the warnings were being printed in the Apache log, where it appeared the information I'm interested, but apache log file is very confusing, and I didnt find a way to filter the data. Commented Jun 3, 2015 at 21:04
  • 1
    you could coordinate the time stamp of the error log with the Apache access log to gain some of the information you want. Commented Jun 3, 2015 at 21:04

1 Answer 1

5

So agreeing to and finalizing all the comments given above, the best approach is to set_error_handler.

I wrote a class for you. I also like to set_exception_handler to have a unified experience and save all the errors in Error::$throwables to display them on shutdown instead of the View (handled by View class not provided here).

class Error
{
    public static $error_types = array(
        E_ERROR => 'E_ERROR',
        E_WARNING => 'E_WARNING',
        E_PARSE => 'E_PARSE',
        E_NOTICE => 'E_NOTICE',
        E_CORE_ERROR => 'E_CORE_ERROR',
        E_CORE_WARNING => 'E_CORE_WARNING',
        E_COMPILE_ERROR => 'E_COMPILE_ERROR',
        E_COMPILE_WARNING => 'E_COMPILE_WARNING',
        E_USER_ERROR => 'E_USER_ERROR',
        E_USER_WARNING => 'E_USER_WARNING',
        E_USER_NOTICE => 'E_USER_NOTICE',
        E_STRICT => 'E_STRICT',
        E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
        E_DEPRECATED => 'E_DEPRECATED',
        E_USER_DEPRECATED => 'E_USER_DEPRECATED'
    );

    public static $shutdown = FALSE;

    public static $throwables = array();

    public static function set_throwable_handlers()
    {
        ini_set('error_reporting', E_ALL & ~E_DEPRECATED & ~E_STRICT);
        ini_set('display_errors', FALSE);
        ini_set('log_errors', TRUE);
        ini_set('error_log', '/path/to/logs/error_log');

        set_error_handler(array('Error', 'error_handler'));
        set_exception_handler(array('Error', 'exception_handler'));

        register_shutdown_function(array('Error', 'shutdown_handler'));
    }

    public static function set_throwable($error_number, $error_text, $error_file, $error_line, $error_log = TRUE)
    {
        if ($error_log === TRUE)
        {
            //provide any data you want to log to error log
            error_log('PHP ' . self::$error_types[$error_number] . ' : ' . $error_text . ' in ' . $error_file . ' on line ' . $error_line);
        }

        //provide any data you want to class variable
        self::$throwables[$error_number][] = array('type' => self::$error_types[$error_number], 'text' => $error_text, 'file' => $error_file, 'line' => $error_line);
    }

    public static function exception_handler(Exception $exception)
    {
        self::set_throwable($exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine());
    }

    public static function error_handler($error_number = '', $error_text = '', $error_file = '', $error_line = '')
    {
        self::set_throwable($error_number, $error_text, $error_file, $error_line);
    }

    public static function shutdown_handler()
    {
        $error = error_get_last();

        if ($error !== NULL)
        {
            self::set_throwable($error['type'], $error['message'], $error['file'], $error['line'], FALSE);
        }

        //enables error page on shutdown & displays the throwables
        //self::$shutdown = TRUE;
        //
        //View::display();
    }

    public static function throw_error($error_text, $error_number = E_USER_NOTICE)
    {
        trigger_error($error_text, $error_number);

        exit;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Where is set_throwable_handlers() called?

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.