Laravel 5 different log levels for development and production

巧了我就是萌 提交于 2019-12-03 11:54:36

This gist shows a more comfortable answer, as is not dependent on the chosen handler.

I'm just providing the essential part in an answer here in case the above link gets deleted in some time.

In the AppServiceProviders' register method:

/**
* Register any application services.
*
* @return void
*/
public function register()
{
    //
    $monolog = Log::getMonolog();
    foreach($monolog->getHandlers() as $handler) {
      $handler->setLevel(Config::get('app.log-level'));
    }
}

Then just add an additional key to your config/app.php:

'log-level' => 'info', // or whatever minimum log level you would like.

Add the following code to your AppServiceProvider::register():

$this->app->configureMonologUsing(function ($monolog) {
  $monolog->pushHandler(
    $handler = new RotatingFileHandler(
       $this->app->storagePath() . '/logs/laravel.log',
       $this->app->make('config')->get('app.log_max_files', 5),
       $this->app->make('config')->get('app.level', 'debug')
     )
  );

  $handler->setFormatter(new LineFormatter(null, null, true, true));
});

This recreates the logic that Laravel does when setting up the daily handler, but adds passing level to the handler.

You can set your minimum logging level by setting level value in your config/app.php:

'level' => 'debug', //debug, info, notice, warning, error, critical, alert, emergency

This is a bit of a workaround and each type of handler would need to be set up separately. I'm currently working on a pull-request to Laravel that would add setting minimum debug level from the config file without writing a line of code in your AppServiceProvider.

The code above hasn't been tested, so let me know if you see any typos or something doesn't work properly and I'll be more than happy to make that work for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!