Php 如何在try/catch语句laravel中记录错误?
我正在执行一个try-catch语句,其中我需要代码来执行一些操作,如果它无法捕获错误,请将其记录到laravel日志文件中,然后继续循环。我的代码是:Php 如何在try/catch语句laravel中记录错误?,php,logging,error-handling,try-catch,laravel-5.2,Php,Logging,Error Handling,Try Catch,Laravel 5.2,我正在执行一个try-catch语句,其中我需要代码来执行一些操作,如果它无法捕获错误,请将其记录到laravel日志文件中,然后继续循环。我的代码是: foreach ($logins as $login) { try { // Do something here } catch (Exception $e) { // Log errors \Log::error( $e->g
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
我在名称空间中使用了\Log::并尝试添加
use Log代码>但是我仍然得到这个错误 在其中一个脚本中,有一个类似于以下内容的命名空间声明:
namespace projects\name;
由于在声明之前还有其他脚本行,因此会触发此错误。这是非法的:命名空间声明必须是第一个执行语句
use projects\name\Log; //early in the file. No need for leading \
...
Log::error(...)
一旦你解决了这个问题,那么这一行:
\Log::error(...)
也会给您造成错误。前导的\
表示您正在访问全局PHP命名空间中的类。如果Log
类位于特定的命名空间中,例如projects\name
,则可以通过以下两种方式之一使用该类。使用完全限定名:
\projects\name\Log::error(...)
或者使用use
语句
use projects\name\Log; //early in the file. No need for leading \
...
Log::error(...)
谢谢,这是我的问题。还必须添加一个\for\Exception$e
,因为laravel有名称空间。Log是laravel根(全局)名称空间中的facade。