Total Control of Logging in Laravel 5.1

A little while back I showed how to configure LogEntries with Laravel 5.0. Now that I’ve updated to 5.1, I needed to update the configuration to use Laravel’s new layout. In addition, I needed to control the level at which log entries were being saved to remove the clutter of the logs in the staging/production environment. Here’s how you set that up.

Laravel-5.png

With Laravel 5.1, they offer a configureMonologUsing method that you can add to your bootstrap/app.php file and customize what and how Monolog is setup. Looking through some of the existing code, the different Monolog handler types support a “log level” parameter. They are just set to “debug” by default in it’s core configuration. What we do then is add a couple of extra variables to our .env file:

LOGGING_LEVEL – Can be set to DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, or ALERT

LOG_MAX_FILES – Maximum files to use in the daily logging format.

From there we add the following code to our bootstrap/app.php file right before the final line that returns the $app variable:


$app->configureMonologUsing(function($monolog) {

   // Configure default file logger to follow level conventions in
   // environment configuration.
   $logLevels = Logger::getLevels();
   $logLevel = env('LOGGING_LEVEL', 'DEBUG');
   $logMaxFiles = (int)env('LOG_MAX_FILES', 5);
   $storagePath = realpath(__DIR__.'/../') . '/storage';

   $monolog->pushHandler(
      $handler = new RotatingFileHandler(
         $storagePath . '/logs/laravel.log',
         $logMaxFiles,
         $logLevels[$logLevel])
   );

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

   // Configure logentries to follow level conventions in environment configuration.
   $logEntriesHandler = new LogEntriesHandler(env('LOGENTRIES_TOKEN'));
   $monolog->pushHandler($logEntriesHandler, $logLevels[$logLevel]);
});

That’s all there is to it. If you compare this to the default code, you may have noticed that I didn’t use the storagePath variable, because that value hasn’t been defined yet. Instead we use the “raw” pathing that the storagePath call when setting itself up.

The final step (and another blog post) will be to allow upating the logging configuration on the fly, something that I like to do with my .Net based apps.

Enjoy!

6 thoughts on “Total Control of Logging in Laravel 5.1

  1. Do you have a step by step setup for laravel. I Have implemented what you have said and I still cannot get it working.

    1. Are you seeing an error or just no data? The update should just require you to put that code in the app.php file. You can always hard code the config values and test env settings later, though env should default to the second value.

  2. I suggest replacing this line:
    env(‘LOGGING_LEVEL’, ‘DEBUG’);

    with
    config(‘app.logging_level’)

    and in config/app.php add
    ‘logging_level’ => env(‘LOGGING_LEVEL’, ‘WARNING’);

    The reason is that using .env in production is not recommended, instead you should use
    artisan config:cache
    and in that case env() will not return anything useful, unless you set LOGGING_LEVEL in global server env variables from outside server settings.

    The reason why default level is set to WARNING and not DEBUG is that I prefer to leave all config values as close to production as possible, and use .env for all other cases.

What are your 10 bits on the matter? I want to know!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s