doulu8446 2012-06-02 22:32
浏览 130
已采纳

使用自定义异常处理程序(set_exception_handler)时,不会自动记录异常

I want to use a custom exception handler (set with set_exception_handler() function) to control what is printed to a user when unhandled exception occurs.

Unfortunately, in this case exception is not logged to a php error log and I don't want to write my own logging code because automatic exception logging was absolutely ok for me.

The question is:
Is there a way to make PHP log exception (as by default) if custom exception handler was executed? If no, is there a way to log into main php error log file?

  • 写回答

1条回答 默认 最新

  • dousu1900 2012-06-02 22:55
    关注

    Is there a way to make PHP log exception (as by default) if custom exception handler was executed?

    No, not from what PHP offers (e.g. return FALSE in the callback).

    If no, is there a way to log into main php error log file?

    Yes, you can use the error_logDocs function for that. The ExceptionDocs should contain the message as well as file name and line number. I don't have any code for that at hand, otherwise I've added/linked it.

    One more or less dirty trick (but I'm not sure if it preserves the original filename and line-number) is to create an exception capturing object that remains in memory all the time. If it captures an uncatched exception, it will handle it and store it. In case of destruction (at the end of the process) it can re-throw it. Then PHP needs to deal with it and probably logs it. However I would consider that experimental.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?