在数据库中存储PHP异常对象

I have need to store a PHP Exception object in a mysql column. It's for an offline error logging system. Usually I would just serialize() the Exception object and be done with it, but half the time, when trying to do that, I get the following error:

Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed'

I am not sure how to get this to work consistently. I will greatly appreciate anyone who has an answer to this problem.

Thanks.

The exception object to be logged contains an instance of Closure class, PHP's implementation of anonymous functions and closure. Apparently anonymous functions cannot be serialized.

You need to investigate your exception classes and see if any of them is supposed to contain them. Normally, exception classes shouldn't have an anonymous function as property.

This reproduces the same error message as your case:

$exception = new Exception('BOO');
$anonymousFunction = function() { echo 'blah'; };
$exception->anonymousFunction = $anonymousFunction;
serialize($exception);

So dig in through your code, your framework's code, your library's code, and try to find out which exception class did have an anonymous function as class property, who assigned them, why - and then you should be able to create a special case for it.

Hope this helps.

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

here's the global error handler definition function. you can define a global error handler and make it write the error description to the database.

And the structure of the exception class :

http://php.net/manual/en/class.exception.php