Laravel 5 different levels of logs for development and production

I am using Laravel 5.1 and trying to set different registration logic for the development and production environment.

In my application, I use the Log facade with most of the following methods:

 Log::emergency($error); Log::alert($error); Log::critical($error); Log::error($error); Log::warning($error); Log::notice($error); Log::info($error); Log::debug($error); 

However, in my work environment, I would only like to log everything that is the priority of Error , Critical , Alert or Emergency and ignore requests for lower priority logs.

I could not find anything in the documentation or by studying the code (both Log facade and Monolog class).

My current thought is to create a custom wrapper around the front of the magazine, which simply checks the environment and ignores anything below 400 (Monologues for error level). Basically, I would create a threshold variable in the environment file, and everything below will simply not register in the files.

Before I do this, I wanted to ask the community if there is an existing method / configuration for what I can use so that I don't reinvent the wheel.

If not, what would be the best approach?

+7
php logging laravel laravel-5 monolog
source share
2 answers

This gist shows a more convenient answer, since it does not depend on the selected handler.

I just provide the bulk of the answer here if the above link is removed after a while.

In the Register method of AppServiceProviders:

 /** * 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. 
+4
source share

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 Laravel logic when setting up the handler daily , but adds a transition level to the handler.

You can set a minimum logging level by setting the level in 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 must be configured separately. I'm currently working on finding a Laravel request that will add a minimal debugging level from the configuration file without writing a line of code in the AppServiceProvider .

The code above has not been tested, so let me know if you see any typos or something is not working properly, and I will be more than happy to do the job for you.

+2
source share

All Articles