Very Urgent: tringger_error() in PHP error handling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
venkateshb
Forum Newbie
Posts: 3
Joined: Thu Mar 26, 2009 3:36 am

Very Urgent: tringger_error() in PHP error handling

Post by venkateshb »

I have created a custom error handling function in PHP.
For that I have created a function called logManager. It is shown in the below code-
The problem I am facing is, when the trigger_error() is called, it is logging the error line and the file name from logManager function and file.
It is Currently showing -
<errorentry>
<datetime>24 Mar 09 - 12:09:26 PM (UTC)</datetime>
<scriptname>D:\error_handler_class\log_manager.php</scriptname>
<scriptlinenum>9</scriptlinenum>

<errortype>E_USER_NOTICE</errortype>
<errornum>1024</errornum>
<errormsg>Entering function f1()</errormsg>
</errorentry>

But the expected result is -
<errorentry>
<datetime>24 Mar 09 - 12:09:26 PM (UTC)</datetime>
<scriptname>D:\current_working\index.php</scriptname>
<scriptlinenum>60</scriptlinenum>

<errortype>E_USER_NOTICE</errortype>
<errornum>1024</errornum>
<errormsg>Entering function f1()</errormsg>
</errorentry>

Note: See the difference in <scriptname></scriptname>
and <scriptlinenum></scriptlinenum>

Code: Select all

 
// File name log_manager.php
 
<?
function logManager($log_msg, $log_method, $error_level){
 $handler = new error_handler();
 set_error_handler(array(&$handler, "handle_custom_error"), E_ALL);
 if($log_method=="trigger_error"){
  [color=#FF0000] trigger_error($log_msg, $error_level);[/color] 
 }
}
 
class error_handler{
 var $log_file="D:/error_handler_class/log_msg.xml"; //Logged messages
 
 //error handling function...
 function handle_custom_error($errno, $errstr, $errfile, $errline, $errcontext){
 $this->errno = $errno;
 $this->errstr = $errstr;
 $this->errfile = $errfile;
 $this->errline = $errline;
 $this->errcontext = $errcontext;
    
 if($this->log_file)
   $this->log_error_msg();
 
   /* Don't execute PHP internal error handler */
   return true;
}
 
 function log_error_msg(){
  // set of errors for which a var trace will be saved
  $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
  $message = "<errorentry>\n";
  $message .=  "\t<datetime>" . date("j M y - g:i:s A (T)", mktime()) . "</datetime>\n";
  $message .= "\t<scriptname>" . $this->errfile . "</scriptname>\n";
  $message .= "\t<scriptlinenum>" . $this->errline . "</scriptlinenum>\n";
  $message .= "\t<errortype>" . $this->error_numbers[$this->errno] . "</errortype>\n";
  $message .= "\t<errornum>" . $this->errno . "</errornum>\n";
  $message .= "\t<errormsg>" . $this->errstr . "</errormsg>\n";
  $message .= "</errorentry>\n\n";
 
  error_log($message, 3, $this->log_file);
 }
}
?>
 
//And In my PHP files I am using this as follows.
// File name index.php
<?
include("./../error_handler_class/log_manager.php");
logManager("","","");
// undefined constant, generates a notice
$t = I_AM_NOT_DEFINED;
//generates a warning
$v = 7/0;
f1();
 
function f1(){
  [color=#FF0000]logManager("Entering function f1()","trigger_error",E_USER_NOTICE); //log user notice[/color]
  echo "Hi";
  $a=10;
  echo $a/0;
  logManager("Exiting function f1()", "trigger_error", E_USER_NOTICE); //log user notice
}
?>
 
 
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Very Urgent: tringger_error() in PHP error handling

Post by php_east »

i usually have to skip two steps to get who is calling my function.

Code: Select all

function tracer()
{
$c  = 0;
$skip   = 2;
foreach (debug_backtrace() as $key=>$value)
        {
        $c++;
        if ($c<=$skip) continue;
        ?>
        <table>
        <tr>
        <td><?php echo(@$value['file']);    ?>&nbsp;</td>
        <td><?php echo(@$value['line']);    ?>&nbsp;</td>
        <td><?php echo(@$value['function']);   ?>&nbsp;</td>
        <td><?php echo(@$value['class']);   ?>&nbsp;</td>
        <td><?php echo(@$value['type']);    ?>&nbsp;</td>
        </tr>
        </table>
        <?php
        break;
        }
}
 
Post Reply