Log caught exception with stack trace

前端 未结 4 1079
逝去的感伤
逝去的感伤 2020-12-24 05:13

If I don\'t catch an exception in PHP, I get a helpful error message in my error.log file with a stack trace. For example, if I run:



        
4条回答
  •  我在风中等你
    2020-12-24 05:41

    error_log($e);
    

    does what you want. It logs exactly the same thing that would have been logged if you didn't catch the exception, minus the word 'Uncaught' at the beginning. It does this because that's what the Exception class's __toString() magic method returns.

    You can do this in a catch block:

    try {
        foo();
    } catch (Exception $e) {
        error_log("Caught $e");
    }
    

    Or in the exception handler:

    set_exception_handler(function($exception) {
        error_log($exception);
        error_page("Something went wrong!");
    });
    

提交回复
热议问题