2015-06-03 44 views
3

我在我的php.ini文件配置error_log指令,像這樣:是否可以更改PHP錯誤日誌輸出?

error_log = /path/to/logs/error_log 

,然後,我配置error_reporting指令是這樣的:

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT 

當我檢查的error_log文件,我看到正常的php警告/錯誤文本行:

[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 

問題是:有一種方法可以改變輸出格式?,我m恩,如果我可以打印,例如,導致警告的IP和子域。

我一直在尋找StackOverflow,在谷歌,我沒有找到明確的信息或例子。

非常感謝您的幫助。

+3

我想,那麼你會寫自己的錯誤日誌功能,只是落實到您的自定義錯誤處理程序 – Rizier123

+1

你必須改變,在PHP源代碼,並編譯自己定製的PHP。您只能控制WHICH消息/警告/錯誤記錄,而不是其文本格式/內容。一般來說,PHP不知道用什麼IP /主機名來調用腳本。該信息可以從$ _SERVER找到,但是PHP本身並不知道web服務器中的映射是如何引起它被調用的。 –

+2

要與@ Rizier123一起,請查看http://php.net/manual/en/function.set-error-handler.php – AbraCadaver

回答

3

所以同意&&敲定上面給出的所有意見最好的方法是set_error_handler

爲你寫了一堂課。我還希望set_exception_handler擁有統一的體驗並保存Error::$throwables中的所有錯誤,以便在關閉時顯示它們而不是視圖(由View類處理,此處未提供)。

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; 
    } 
} 
+1

我認爲這是最好的方法!非常感謝! –