2.PHP如何記錄錯誤和發送錯誤

來源:互聯網
上載者:User
1. 將錯誤記錄到指定的檔案中,配置過程如下
* 1). log_errors=on
* 2). error_log=/tmp/php_error.log
1.使用error_log()函數記錄錯誤記錄檔
error_log(‘要記錄的錯誤資訊!’);

header('content-type:text/html; charset=utf-8');//開啟所有的錯誤報表error_reporting(-1);//設定時區ini_set('date.timezone', 'PRC');//禁用頁面顯示錯誤ini_set('display_errors', 0);//開啟日誌記錄功能ini_set('log_errors', 1);//設定錯誤記錄檔儲存的位置ini_set('error_log', '/tmp/custom_error.log');//忽略重複的錯誤ini_set('ignore_repeated_errors', 'on');//忽略重複的錯誤來源ini_set('ignore_repeated_source', 'on');error_log('我要把你記錄到我的自訂錯誤記錄檔中'.date('Y-m-d H:i:s', time()));trigger_error('trigger_error-----我要把你記錄到我的系統錯誤記錄檔中'.date('Y-m-d H:i:s', time()), E_USER_NOTICE);settype($var, 'king');

2.將錯誤記錄到系統日誌中
* 1.ini_set('error_log', 'syslog');常用

方法一//開啟所有的錯誤報表error_reporting(-1);//禁用頁面顯示錯誤ini_set('display_errors', 0);//開啟日誌記錄功能ini_set('log_errors', 1);//設定錯誤記錄檔儲存的位置------(系統日誌)ini_set('error_log', 'syslog');//忽略重複的錯誤ini_set('ignore_repeated_errors', 'on');//忽略重複的錯誤來源ini_set('ignore_repeated_source', 'on');// error_log('error_log-----我要把你記錄到我的系統錯誤記錄檔中'.date('Y-m-d H:i:s', time()));trigger_error('trigger_error-----我要把你記錄到我的系統錯誤記錄檔中'.date('Y-m-d H:i:s', time()), E_USER_NOTICE);// settype($var, 'king');方法二//====================出於安全問題不用===========================//開啟到系統日誌的連結openlog('PHP5.6.0', LOG_PID, LOG_SYSLOG);syslog(LOG_ERR, 'this is a test syslog'.date('Y-m-d H:i:s', time()));closelog();

2.將錯誤記錄檔通過郵件發送
* 1.error_log('msg', 1, mail_addr); 第二個參數設定為1
** bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )

message應該被記錄的錯誤資訊。message_type設定錯誤應該發送到何處。可能的資訊類型有以下幾個:error_log() 日誌類型0   message 發送到 PHP 的系統日誌,使用 作業系統的日誌機制或者一個檔案,取決於 error_log 指令設定了什麼。 這是個預設的選項。1   message 發送到參數 destination 設定的郵件地址。 第四個參數 extra_headers 只有在這個類型裡才會被用到。2   不再是一個選項。3   message 被發送到位置為 destination 的檔案裡。 字元 message 不會預設被當做新的一行。4   message 直接發送到 SAPI 的Tlog程式中。destination目標。它的含義描述於以上,由 message_type 參數所決定。extra_headers額外的頭。當 message_type 設定為 1 的時候使用。 該資訊類型使用了 mail() 的同一個內建函數。
//開啟所有的錯誤報表error_reporting(-1);//禁用頁面顯示錯誤ini_set('display_errors', 0);//開啟日誌記錄功能ini_set('log_errors', 1);//忽略重複的錯誤ini_set('ignore_repeated_errors', 'on');//忽略重複的錯誤來源ini_set('ignore_repeated_source', 'on');//設定第二個參數為1將,使用郵件發送錯誤記錄檔資訊error_log('error_log-----我要把你記錄到我的系統錯誤記錄檔中'.date('Y-m-d H:i:s', time()), 1, '732578448@qq.com');

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.