Laravel 5 different log levels for development and production

后端 未结 2 1155
[愿得一人]
[愿得一人] 2021-01-02 02:41

I\'m using Laravel 5.1 and trying to set different logging logic for a development and production environment.

Throughout my application I am using the Log

2条回答
  •  北海茫月
    2021-01-02 03:11

    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.

提交回复
热议问题