我执行了一条try catch语句,在该语句中我需要代码来执行某些操作,如果失败,则捕获错误,将其记录到laravel日志文件中,然后继续循环。我的代码是:
foreach ($logins as $login) {
try {
// Do something here
} catch (Exception $e) {
// Log errors
\Log::error( $e->getMessage() );
continue;
}
}
但我收到一个错误,内容为
[Symfony\Component\Debug\Exception\FatalErrorException]
Namespace declaration statement has to be the very first statement in the script
我在 namespace 中使用了\ Log::,还尝试添加
use Log;
,但仍然出现此错误。
最佳答案
在其中一个脚本中,有一个类似于以下内容的 namespace 声明:
namespace projects\name;
触发错误是因为声明之前还有其他脚本行。那是非法的: namespace 声明必须是第一个执行语句。
解决此问题后,此行:
\Log::error(...)
也会导致您出错。前导的
\
表示您正在访问全局PHP namespace 中的类。如果Log
类位于特定的 namespace 中,例如projects\name
,则可以通过以下两种方式之一使用该类。使用标准名称:\projects\name\Log::error(...)
或使用
use
语句。use projects\name\Log; //early in the file. No need for leading \
...
Log::error(...)
关于php - 如何在try/catch语句laravel中记录错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38548029/