大家肯定有碰到過檢視日誌錯誤資訊時不知道具體是在哪個檔案哪一行出錯,該方法能詳細的顯示出錯誤的具體檔案資訊與行數,給有需要的人使用
預設日誌
[2018-04-11 10:25:29.313456] local.INFO: 我是來測試日誌的
1、在Providers
目錄建立LogServiceProvider.php
檔案 程式碼如下
<?php
namespace App\Providers;
use Carbon\Laravel\ServiceProvider;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Processor\IntrospectionProcessor;
class LogServiceProvider extends ServiceProvider
{
public function boot()
{
$handlers[] = (new RotatingFileHandler(storage_path('logs/lumen.log'), 0))
->pushProcessor(new IntrospectionProcessor())
->setFormatter(new LineFormatter(null, null, true, true));
$this->app['log']->setHandlers($handlers);
}
}
2、在bootstrap/app.php
註冊該服服
$app->register(App\Providers\LogServiceProvider::class);
3、使用測試
app('log')->info('我是測試日誌');
4、生成的日誌資訊
[2018-04-11 10:25:29.313456] local.INFO: 我是來測試日誌的 {"content":1111} {"pid":1340,"file":"App\\Http\\Controllers\\TestController->log:20","line":20,"ip":"127.0.0.1"}
[2018-04-11 10:25:29.317710] local.DEBUG: 我是來測試日誌的 {"pid":1340,"file":"App\\Http\\Controllers\\TestController->log:21","line":21,"ip":"127.0.0.1"}
[2018-04-11 10:25:29.320648] local.ERROR: 我是來測試日誌的 {"pid":1340,"file":"App\\Http\\Controllers\\TestController->log:22","line":22,"ip":"127.0.0.1"}
[2018-04-11 10:25:29.324702] local.WARNING: 我是來測試日誌的 {"pid":1340,"file":"App\\Http\\Controllers\\TestController->log:23","line":23,"ip":"127.0.0.1"}