Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    Question Unable to log "Parse Errors"

    Hi

    I am using the following code to log all types of errors viz. Fatal Error, Warning Error, Notice Error.

    Code:
    set_error_handler("errorHandler");
    register_shutdown_function("shutdownHandler");
    
    function errorHandler($error_level, $error_message, $error_file, $error_line, $error_context)
    {
    $error = "lvl: " . $error_level . " | msg:" . $error_message . " | file:" . $error_file . " | ln:" . $error_line;
    switch ($error_level) {
        case E_ERROR:
        case E_CORE_ERROR:
        case E_COMPILE_ERROR:
        case E_PARSE:
            log($error, "fatal");
            break;
        case E_USER_ERROR:
        case E_RECOVERABLE_ERROR:
            log($error, "error");
            break;
        case E_WARNING:
        case E_CORE_WARNING:
        case E_COMPILE_WARNING:
        case E_USER_WARNING:
            log($error, "warn");
            break;
        case E_NOTICE:
        case E_USER_NOTICE:
            log($error, "info");
            break;
        case E_STRICT:
            log($error, "debug");
            break;
        default:
            log($error, "warn");
    }
    }
    
    function shutdownHandler() //will be called when php script ends.
    {
    $lasterror = error_get_last();
    switch ($lasterror['type'])
    {
        case E_ERROR:
        case E_CORE_ERROR:
        case E_COMPILE_ERROR:
        case E_USER_ERROR:
        case E_RECOVERABLE_ERROR:
        case E_CORE_WARNING:
        case E_COMPILE_WARNING:
        case E_PARSE:
            $error = "[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" . $lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" . $lasterror['line'];
            log($error, "fatal");
    }
    }
    
    function log($error, $errlvl)
    {
      //Log errors
    }

    It works for all except when its a parse error. Try the following example to check what I mean ...


    Code:
    set_error_handler("errorHandler");
    register_shutdown_function("shutdownHandler");
    
    function errorHandler($error_level, $error_message, $error_file, $error_line, $error_context)
    {
    $error = "lvl: " . $error_level . " | msg:" . $error_message . " | file:" . $error_file . " | ln:" . $error_line;
    switch ($error_level) {
        case E_ERROR:
        case E_CORE_ERROR:
        case E_COMPILE_ERROR:
        case E_PARSE:
            log($error, "fatal");
            break;
        case E_USER_ERROR:
        case E_RECOVERABLE_ERROR:
            log($error, "error");
            break;
        case E_WARNING:
        case E_CORE_WARNING:
        case E_COMPILE_WARNING:
        case E_USER_WARNING:
            log($error, "warn");
            break;
        case E_NOTICE:
        case E_USER_NOTICE:
            log($error, "info");
            break;
        case E_STRICT:
            log($error, "debug");
            break;
        default:
            log($error, "warn");
    }
    }
    
    function shutdownHandler() //will be called when php script ends.
    {
    $lasterror = error_get_last();
    switch ($lasterror['type'])
    {
        case E_ERROR:
        case E_CORE_ERROR:
        case E_COMPILE_ERROR:
        case E_USER_ERROR:
        case E_RECOVERABLE_ERROR:
        case E_CORE_WARNING:
        case E_COMPILE_WARNING:
        case E_PARSE:
            $error = "[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" . $lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" . $lasterror['line'];
            log($error, "fatal");
    }
    }
    
    function log($error, $errlvl)
    {
      //Log errors
    }
    
    // Note the missing semicolon (;) after Dog
    echo "Cat";
    echo "Dog"
    echo "Lion";

    Can someone please tell me how do I make it log parse errors as well?


    Thanks in advance

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,122
    Thanks
    2
    Thanked 327 Times in 319 Posts
    Parse errors in your main file are detected during the 'parse phase' of the php language engine (php is a parsed, tokenized, interpreted language) and prevent your code from even running, therefore the code setting the error handler never runs because the interpretation/execution phase is never reached.

    Your code should work for parse errors in included/required code though.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    Question

    Quote Originally Posted by CFMaBiSmAd View Post
    Your code should work for parse errors in included/required code though.


    I tried doing the following but still not working.

    PHP Code:
    <?php
    //test.php
    set_error_handler("errorHandler");
    register_shutdown_function("shutdownHandler");
    error_reporting(0);

    function 
    errorHandler($error_level$error_message$error_file$error_line$error_context)
    {
    $error "lvl: " $error_level " | msg:" $error_message " | file:" $error_file " | ln:" $error_line;

    echo 
    "Level: " $error_level;

    switch (
    $error_level) {
        case 
    E_ERROR:
        case 
    E_CORE_ERROR:
        case 
    E_COMPILE_ERROR:
        case 
    E_PARSE:
        case 
    E_USER_ERROR:
            
    logMe($error"fatal");
            break;
        case 
    E_USER_ERROR:
        case 
    E_RECOVERABLE_ERROR:
            
    logMe($error"error");
            break;
        case 
    E_WARNING:
        case 
    E_CORE_WARNING:
        case 
    E_COMPILE_WARNING:
        case 
    E_USER_WARNING:
            
    logMe($error"warn");
            break;
        case 
    E_NOTICE:
        case 
    E_USER_NOTICE:
            
    logMe($error"info");
            break;
        case 
    E_STRICT:
            
    logMe($error"debug");
            break;
        default:
            
    logMe($error"warn");
    }
    }

    function 
    shutdownHandler() //will be called when php script ends.
    {
      
    $lasterror error_get_last();

      echo 
    "Level: " $lasterror;
      switch (
    $lasterror['type'])
      {
          case 
    E_ERROR:
          case 
    E_CORE_ERROR:
          case 
    E_COMPILE_ERROR:
          case 
    E_USER_ERROR:
          case 
    E_RECOVERABLE_ERROR:
          case 
    E_CORE_WARNING:
          case 
    E_COMPILE_WARNING:
          case 
    E_PARSE:
              
    $error "[SHUTDOWN] lvl:" $lasterror['type'] . " | msg:" $lasterror['message'] . " | file:" $lasterror['file'] . " | ln:" $lasterror['line'];
              
    logMe($error"fatal");
      }
    }

    function 
    logMe($error$errlvl)
    {
       echo 
    'Error No: ' $error ' <BR> Error Level: ' .$errlvl;
    }
    PHP Code:

    <?php
    //index.php
    require "test.php";

    echo 
    "Cat";
    echo 
    "Dog"
    echo "Lion";
    Last edited by phantom007; 05-11-2014 at 06:42 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •