2012-10-04 78 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

相关问题