2012-10-04 77 views
0

我正在開發一個php應用程序,並且爲它創建了日誌記錄功能。我的意思是每次有什麼東西都不對,我把它寫在我的日誌文件中,所以如果用戶報告了錯誤,我可以使用我的日誌文件來追蹤這個錯誤。在我的應用程序日誌文件中寫入PHP日誌

問題是,如果我上傳我的項目生產服務器上,我必須禁用PHP的默認錯誤顯示;我只想在自己的日誌文件中記錄php錯誤,並將其記錄在webserver的'error.log'旁邊。有什麼辦法可以做到嗎?

+0

如果你只是關閉error_reporting,PHP錯誤仍應寫入apache error.log文件。 –

+0

@Jonathan我知道,但我希望PHP在我自己的日誌文件中寫入錯誤。 –

+4

http://www.php.net/manual/en/function.set-error-handler.php –

回答

2

對於精選保質異常(我打電話全功能使用的例外,(不包括堆棧跟蹤)):

設置,如果你是開發商:

define('EXCEPTION_HANDLER_ACTIVE', true); 

在生產中,這也應該是真實的,但修改了代碼,不會顯示最終用戶的錯誤。 (使用:error_reporting(0); ini_set('display_errors', false),頁面頂部));爲處理異常

第一組主要功能:

set_error_handler("_handler_exception"); 

函數調用異常處理程序:

function _handler_exception($err_severity, $err_msg, $err_filepath, $err_line) 
    { 
     if (EXCEPTION_HANDLER_ACTIVE == 1) 
     { 
      $eh = new exceptionhandler; 

      $eh->exception_active = true; 
      $eh->set_exception_handler_log($err_severity, $err_msg, $err_filepath, $err_line); 
     } 
     else 
     { 
      $eh = new exceptionhandler; 

      $eh->exception_active = false; 
      $eh->set_exception_handler_log($err_severity, $err_msg, $err_filepath, $err_line); 
     } 
    } 

類的ExceptionHandler:

class exceptionhandler 
{ 
    public $exception_active = true; 

    public function set_exception_handler_log($err_severity, $err_msg, $err_filepath, $err_line) 
    {   
    $ret_err_severity = NULL; 
    $ret_err_no = NULL; 
    $ret_info = NULL; 
    $filenameerr = NULL; 
    $dbout = NULL; 

    switch ($err_severity) 
    { 
     case E_ERROR: 
      $ret_err_severity = "E_ERROR"." [ ".$err_severity." ]"; 
      $ret_err_no = "1"; 
      $ret_info = "Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted."; 
      $ret_err_msg = $err_msg; 
      break; 

     case E_STRICT: 
      $ret_err_severity = "E_STRICT"." [ ".$err_severity." ]"; 
      $ret_err_no = "2048"; 
      $ret_info = "Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. "; 
      $ret_err_msg = $err_msg; 
      break; 
     default: 
      $ret_err_severity = "CODE"." [ ".$err_severity." ]"; 
      $ret_err_no = $err_severity; 
      $ret_info = "Other error"; 
      $ret_err_msg = $err_msg; 
      break; 
    } 

    $ef = explode("/", $err_filepath); 
    $ec = (count($ef) - 1); 
    $filenameerr = $ef[$ec]; 

    $backtrace = debug_backtrace(); 

    if (!isset($doc_root)) { 
     $doc_root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']); 
    } 

    $debug_backtrace_parse = print_r(debug_backtrace(), true); 
    $debug_backtrace2 = str_replace($err_msg, '<b>'.$err_msg.'</b>', $debug_backtrace_parse); 
    $debug_backtrace = str_replace($err_filepath, '<b>'.$err_filepath.'</b>', $debug_backtrace2); 

    $line = (isset($backtrace[1]['line'])) ? htmlspecialchars($backtrace[1]['line']) : ''; 
    $file = (isset($backtrace[1]['file'])) ? htmlspecialchars(str_replace(array('\\', $doc_root), array('/', ''), $backtrace[1]['file'])) : ''; 
    $class = !empty($backtrace[2]['class']) ? htmlspecialchars($backtrace[2]['class']) . '::' : ''; 
    $function = !empty($backtrace[2]['function']) ? htmlspecialchars($backtrace[2]['function']) . '() ' : ''; 
    $dbout = "<pre>$class$function =&gt;$file #$line</b><pre>"; 

    $memory = memory_get_usage()/1024/1024; 

    if ($this->exception_active == true) 
    { 
     $source_highlight = $this->source_highlight($file, $backtrace); 

     ob_start(); 
     include_once(exception.tpl); 
     $buffer = ob_get_contents(); 
     ob_end_clean(); 
     echo $buffer; 
     exit(-1); 
    } 
    } 

Exception.tpl

<html> 
    <head> 
    <title><?php echo $ret_err_severity; ?> | <?php echo $filenameerr; ?></title> 
    <style type="text/css"> 
     div#errorhandlerexception 
     { 
      border: 1px solid #ff0000; 
      background-color: #ff9999; 
      width: 96%; 
      padding: 10px; 
      color: #000; 
      font-family: sans-serif; 
      font-size: 10px; 
      margin: 0 auto; 
     } 
     div#errorhandlerexception h2 
     { 
      border-bottom: 1px solid #fff; 
      color: #ffffff; 
     } 
     div#errorhandlerexception td 
     { 
      background-color: #ffcccc; 
      width: 100%; 
      padding: 3px; 
     } 
     div#errorhandlerexception .main 
     { 
      width: 100px; 
      height: 30px; 
     } 
     div#errorhandlerexception 
     { 
      margin-bottom: 40px; 
     } 
     .linenum 
     { 
      text-align:right; 
      background:#FDECE1; 
      border:1px solid #cc6666; 
      padding:0px 1px 0px 9px; 
      float:left; 
      width:25px; 
      margin:3px 0px 30px 0px; 
      font-family: Courier New, Courier; 
      font-size: 11px; 
     } 
     .code 
     { 
      font-family:Courier New, Courier; 
      font-size: 11px; 
      float: left; 
      width: 100%; 
     } 
     .linetext 
     { 
      width:95%; 
      text-align:left; 
      background: #fff; 
      border: 1px solid #cc6666; 
      border-left:0px; 
      padding:0px 1px 0px 8px; 
      font-family: Courier New, Courier; 
      float:left; 
      margin:3px 0px 30px 0px; 
      font-size: 11px; 
     } 
     br.clear 
     { 
      font-family:Courier New, Courier; 
      clear:both; 
      font-size: 11px; 
     } 
     #exception_selected_block 
     { 
      background-color: #ffff00; 
     } 
     div.exception_filename 
     { 
      border-left: 19px solid #AA7777; 
      border-top: 3px solid #AA7777; 
      color: #000000; 
      float: left; 
      font-weight: bold; 
      padding: 6px; 
      width: 98%; 
     } 
    </style> 
    </head> 

<body> 
<div id="errorhandlerexception"><?php #echo $msg; ?> 
<h2>EXCEPTION ERROR HANDLER</h2> 
<table> 
    <tr><td class="main">Status:</td><td><?php echo $ret_err_severity; echo " #".$err_severity; ?></td></tr> 
    <tr><td class="main">Debug stack:</td><td><?php echo $dbout; ?></td></tr> 
    <tr><td class="main">Code:</td><td><?php echo $ret_err_no; ?></td></tr> 
    <tr><td class="main">Message:</td><td><?php echo $ret_err_msg; ?></td></tr> 
    <tr><td class="main">Info:</td><td><?php echo $ret_info; ?></td></tr> 
    <tr><td class="main">File:</td><td><?php echo $filenameerr; ?><br><?php echo $err_filepath; ?></td></tr> 
    <tr><td class="main">Line:</td><td><?php echo $err_line; ?></td></tr> 
    <tr><td class="main" colspan="2"><?php echo $source_highlight; ?></td></tr> 
    <tr><td class="main">Debug backtrace:</td><td><pre><?php echo $debug_backtrace; ?></pre></td></tr> 
</table> 
</div> 
</body> 
</html> 
3

您可以使用自定義錯誤處理程序在php中執行自己的日誌記錄。這也可以讓你登錄到數據庫(假設錯誤不是數據庫相關的,並且你有一個有效的連接)或者基於錯誤的平面文件並且還設置自定義錯誤頁面。見:

http://www.php.net/manual/en/function.set-error-handler.php

1

可以使用的set_error_han dler來定義自己的功能,這是在錯誤的情況下調用。

<?php 
//error handler function 
function customError($errno, $errstr, $errfile, $errline) 
    { 
    echo "<b>Custom error:</b> [$errno] $errstr<br />"; 
    echo " Error on line $errline in $errfile<br />"; 
    echo "Ending Script"; 
    die(); 

//set error handler 
set_error_handler("customError"); 

$test=2; 

//trigger error 
if ($test>1) 
    { 
    trigger_error("A custom error has been triggered"); 
    } 
?> 

編號:http://www.w3schools.com/php/func_error_set_error_handler.asp

相關問題