【发布时间】:2018-08-15 00:13:30
【问题描述】:
Laravel 在捕获异常时具有可读的日志和堆栈跟踪,例如:
production.ERROR: Command "test" is not defined.
Did you mean this?
make:test {"exception":"[object] (Symfony\\Component\\Console\\Exception\\CommandNotFoundException(code: 0): Command \"test\" is not defined.
Did you mean this?
make:test at {root}/vendor/symfony/console/Application.php:618)
[stacktrace]
#0 {root}/vendor/symfony/console/Application.php(229): Symfony\\Component\\Console\\Application->find('test')
#1 {root}/vendor/symfony/console/Application.php(148): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#2 {root}/vendor/laravel/framework/src/Illuminate/Console/Application.php(88): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#3 {root}/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(121): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#4 {root}/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#5 {main}
"}
问题是:是否可以自己捕获异常,并记录相同格式的stacktrace,并继续程序执行。到目前为止,我正在通过Log::error(json_encode(debug_backtrace())); 记录错误,这非常丑陋且难以追踪。示例代码:
try {
foo();
} catch(\Exception $e) {
Log::error(json_encode(debug_backtrace()));
}
bar();
【问题讨论】:
-
这里是记录 debug_backtrace
\Log::error(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 15));的更好方法 -
不需要'json_encode'
标签: php laravel exception error-handling