2012-08-05 69 views
1

我正在使用mPDF類從HTML生成PDF。禁用CodeIgniter中特定幫助程序/庫的錯誤日誌記錄

儘管PDFs完全像他們應該顯示的一樣,但我的CodeIgniter錯誤日誌中卻擠滿了似乎是由於mPDF中的一些錯誤而引起的錯誤通知。

由於這些通知是無害的,並且PDF格式完美無缺,所以我希望在運行此類時專門禁用CodeIgniter錯誤日誌記錄。

但我還沒有找到辦法做到這一點。

這裏是我的代碼:

控制器

$this->load->helper('mpdf'); 
mpdf($html, $filename); 

助手(mpdf_helper.php)

function mpdf($html, $filename) 
{ 
    $CI =& get_instance(); 
    $CI->config->set_item('log_threshold', 0); 
    include('mpdf/mpdf.php'); 

    $mpdf=new mPDF('', 'letter'); 
    $mpdf->SetHTMLHeader('powered by example.com'); 
    $mpdf->WriteHTML($html, 0); 
    $mpdf->Output($filename, 'I'); 
} 

正如你可以看到我想要的log_threshold手動設置的配置,以0,但這不會阻止錯誤記錄。

FYI我index.php

define('ENVIRONMENT', 'production'); 

這臺error_reporting(0)

你知道我應該怎麼做才能停止CodeIgniter記錄錯誤,只有當我運行mPDF?

錯誤示例

ERROR - 2012-08-04 23:03:59 --> Severity: Notice --> Undefined index: direction /var/www/vhosts/asd.com/httpdocs/application/helpers/mpdf/mpdf.php 21103 

ERROR - 2012-08-04 23:06:07 --> Severity: Notice --> Undefined index: MARGIN-TOP /var/www/vhosts/asd.com/httpdocs/application/helpers/mpdf/mpdf.php 17271 
+0

你可以發佈你收到的錯誤嗎?你不能只爲一個區域改變error_reporting - 這是一個系統範圍的功能 – Laurence 2012-08-05 03:08:02

+0

這些通常是「未定義的索引」錯誤,不影響PDF輸出 - 我在OP中增加了一個例子 - 任何想法我可以選擇性地抑制錯誤? – pepe 2012-08-05 12:33:14

回答

3

這是因爲CodeIgniter的錯誤日誌迷上了set_error_handler,和PHP將火處理器無論使用error_reporting值。正如PHP手冊所說:

...但是您仍然能夠讀取error_reporting的當前值並採取適當的行動。

如果CI的內部錯誤級別要求記錄,CI的默認錯誤處理程序不會記錄日誌(但會顯示錯誤),just logs everything that comes in。 (該log_threshold配置值1,see the function由PHP錯誤處理程序調用。)

如果你想使CI記錄尊重error_logging水平,你可以用standard method這樣的擴展CI_Exception類:

class MY_Exceptions extends CI_Exceptions { 
    function log_exception($severity, $message, $filepath, $line) { 
     $current_reporting = error_reporting(); 
     $should_report = $current_reporting & $serverity; 

     if ($shoud_report) { 
      // call the original implementation if we should report the error 
      parent::log_exception($severity, $message, $filepath, $line); 
     } 
    } 
} 

一旦你得到了這個地方,你可以在你的庫調用周圍或在庫調用中調用error_reporting。

+0

這看起來像一個很好的解決方案 - 我怎樣才能使用這個功能(即,我應該在哪裏應該在我的OP代碼中調用它)? – pepe 2012-08-05 14:23:52

+0

擴展核心庫的工作方式是在'application/libraries'或'application/core'下創建一個適當命名的文件,參見[documentation]的擴展核心類部分(http:// codeigniter。 COM/user_guide /一般/ core_classes.html)。在你的案例中,將'MY_Exceptions'類代碼複製到'application/core/MY_Exceptions.php'文件中,並且CI應該自動提取它(注意大寫/小寫字符)。 – complex857 2012-08-05 15:54:00