Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 different log levels for development and production

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 facade with most of the following different 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 production environment, I would like to only log anything that is an Error, Critical, Alert or Emergency priority and ignore log requests with lower priority.

I couldn't find anything in the documentation or by exploring the code (both Log facade and the Monolog class).

My current thought is to create a custom wrapper around the Log facade that simply checks the environment and ignores anything below 400 (Monolog level for Error). Basically I would create a threshold variable in the environment file and anything below it will simply not be logged to the files.

Before I do so, I wanted to ask the community if there is an existing method/configuration for that which I could use, so that I don't re-invent the wheel.

If not - what would be the best approach?

like image 740
dev7 Avatar asked Aug 23 '15 15:08

dev7


People also ask

What are the five logging levels?

Logging levels explained. The most common logging levels include FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL, and OFF.

Which log level should be used in production?

The WARN level is the level that should be active in production systems by default, so that only WARN and ERROR messages are being reported, thus saving storage capacity and performance.

What is logs in Laravel?

Laravel logging is based on "channels". Each channel represents a specific way of writing log information. For example, the single channel writes log files to a single log file, while the slack channel sends log messages to Slack. Log messages may be written to multiple channels based on their severity.


2 Answers

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.

like image 57
jedrzej.kurylo Avatar answered Sep 28 '22 07:09

jedrzej.kurylo


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.
like image 44
shock_gone_wild Avatar answered Sep 28 '22 07:09

shock_gone_wild