Is it possible to change PHP error log output?

前端 未结 1 793
醉酒成梦
醉酒成梦 2021-01-19 15:22

I\'m configured error_log directive in my php.ini file, like this:

error_log = /path/to/logs/error_log

and then, I configured

相关标签:
1条回答
  • 2021-01-19 16:04

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

    Wrote a class for you. I also like to set_exception_handler to have 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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题