如何在try/catch语句laravel中记录错误


How to log errors in try/catch statement laravel?

我正在执行一个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 

我在名称空间中使用了''Log::,并尝试添加use Log;,但仍然出现此错误。

在其中一个脚本中,有一个类似于以下的命名空间声明:

namespace projects'name;

由于声明之前还有其他脚本行,因此触发了该错误。这是非法的:命名空间声明必须是第一个执行语句。

一旦你解决了这个问题,那么这行:

'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(...)