views:

52

answers:

3

Hi,

In a previous ticket i asked about logging PHP errors in MySQL which gives me:

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
         // mysql connect etc here...
    $sql = "INSERT INTO `error_log` SET
            `number` = ".mysql_real_escape_string($errno).",
            `string` = ".mysql_real_escape_string($errstr).",
            `file` = ".mysql_real_escape_string($errfile).",
            `line` = ".mysql_real_escape_string($errline);
    mysql_query($sql);
    // Don't execute PHP internal error handler
    return true;
}

// set to the user defined error handler 
$new_error_handler = set_error_handler("myErrorHandler");

I can make this work but only if it is triggerred like this:

trigger_error("message here");

However, I also want the error handler to be called for all errors such as syntax errors like:

echo "foo;

But these errors are just outputted to the screen, what am i doing wrong?

+2  A: 

If You're forced to test if syntax is correct, You can use php_check_syntax function, with filename parameter PHP Manual php_check_syntax

php_check_syntax also provides second parameter, witch when used will be populated by the error string, as far as i remember

canni
I ofen use php_check_syntax in magic autoload function, when code is in development, it helps a lot in many problems, uncatchable in some circumstances (frameworks error handling is far from good) :)
canni
+2  A: 

You can only handle runtime errors with a custom error handler. The echo "foo error in your example happens when parsing (i.e. reading in) the source. Since PHP can not fully parse the code, it can also not run your error handler on this error.

Sjoerd
+1  A: 

That's indeed terrible way of error logging

  1. You don't need not a single advantage of a database. Would you make a database lookup for the certain line number? Or order your results by file name?
  2. database is a subject of many errors itself.
  3. You've been told already that it's impossible to catch a parse error at the program logic level, because a syntactically wrong program will never run.

Let's take your code as an example. It will raise a MySQL error (because of poorly formed query) which you will never see. As well as any other errors occurred. That's what I am talking about.

Col. Shrapnel
Thanks for the infomation. It wasnt obvious to me which is why i asked so apologies for not knowing how stupid the question was!. i would say that one clear advantage of logging errors in a database over a flat file is the ability to query the error log in a simple manner. For example - all errors in file x within a date range. There is no simple way to achieve this within the error_log afaik and i've never seen a good error_log parser that isnt massively intensive.
seengee
@snaken Man, you're taking errors wrong. It isn't a collection of arts or something. It isn't something to admire. But for immediate correction. Don't collect errors, just repair it!
Col. Shrapnel
that is true, errors should be fixed but then the same could be said about a traditional error log couldnt it? All it is doing is collecting errors not fixing them.
seengee
@snaken it is collected just to inform a webmaster. not to attract him. no need to pick between them. just get it one by one and correct. log do not "collect" errors. It just holds them, while webmaster is not online.
Col. Shrapnel
I think you live in a bit of a dreamworld where an admin goes through the log error by error fixing them
seengee
@snaken I am doing it this way. I never collect thousands of errors to analyse them. I correct them as soon as they appear - after site update or some accident happened. but you are free to think yourself a smartass and store errors in the database, unlike all other developers do
Col. Shrapnel
not thinking of myself as a "smartass" in any way at all. i'm not even suggesting this is a better way of doing it. all i'm doing is looking at the option of doing it in certain circumstances. If you have ever had a server with 100+ sites all sharing the same error log then you would likely understand.
seengee
@snaken I can't believe you have it either. A programmer, responsible for such a server, is able to write 5 lines long log parser to analyze it either way or store it in the database, without asking questions on SO. And does not make such a silly mistakes in SQL queries as in one you present here. It turns out not me living in a dreamworld, eh? ;)
Col. Shrapnel
@Col. - there's no point. the SQL query was thrown up as an example, sorry this has caused you so much offence!. This whole thing was a proof of concept which you seem intent on turning into a means of showing how clever you are. I asked a question here to get an answer, not to get abused by you. Log parsers on big log files are massively intensive, much more so than SQL queries but of course you know that already.
seengee
@snaken Dude. if your sites generate such huge logs that makes it parsing hard (i.e. same amount as access logs), you are in trouble. Indeed in trouble. And log parsing is not that trouble. As for your question - sometimes we have to change our mind, based on the new information, not clung to it to death. Sometimes answers is not that we like. It's a life. I am here not to entertain you. I am here to share my knowledge.
Col. Shrapnel