Explanation of how to customize logging behavior in Laravel 5.5

  • 2021-10-27 06:36:38
  • OfStack

Preface

Logging behavior can be easily customized in Laravel 5.6, However, in versions below 5.5, the freedom of log behavior customization is not very high, but the project cannot be forcibly upgraded to 5.6 because of this. Besides, as a stable project upgrade framework, there may be many pits in the large version. For these reasons, I tried to modify the log of Laravel 5.5 to meet my needs.

Most of the logging behavior of Laravel is in Illuminate\ Log\ LogServiceProvider, and we can look at the code snippet in 1:


/**
 * Configure the Monolog handlers for the application.
 *
 * @param \Illuminate\Log\Writer $log
 * @return void
 */
protected function configureDailyHandler(Writer $log)
{
 $log->useDailyFiles(
  $this->app->storagePath().'/logs/laravel.log', $this->maxFiles(),
  $this->logLevel()
 );
}

This is the log storage method I use most often in projects. I can see that the log storage path is almost write-to-death and cannot be easily changed by external parameters.

Initially I wanted to rewrite the Provider and register it in the providers array of app. php, but this behavior is not feasible because, by looking at the source code, LogServiceProvider is registered when the framework starts.

There is one method in which this registration behavior is controlled:


protected function registerBaseServiceProviders()
{
 $this->register(new EventServiceProvider($this));

 $this->register(new LogServiceProvider($this));

 $this->register(new RoutingServiceProvider($this));
}

Now that we know how they work, let's inherit these two classes and modify the behaviors we need to change. My transformation is as follows. Create a new LogServiceProvider class in app\ Providers that inherits Illuminate\ Log\ LogServiceProvider with the following code:


<?php


namespace App\Providers;

use Illuminate\Log\LogServiceProvider as BaseLogServiceProvider;
use Illuminate\Log\Writer;

class LogServiceProvider extends BaseLogServiceProvider
{
 /**
  * Configure the Monolog handlers for the application.
  *
  * @param \Illuminate\Log\Writer $log
  * @return void
  */
 protected function configureDailyHandler(Writer $log)
 {
  $path = config('app.log_path');
  $log->useDailyFiles(
   $path, $this->maxFiles(),
   $this->logLevel()
  );
 }
}

Add configuration in the config/app. php directory:


'log_path' => env('APP_LOG_PATH', storage_path('/logs/laravel.log')),

Create a new Foundation directory in the app directory, create a new Application class that inherits the Illuminate\ Foundation\ Application class, and override the registerBaseServiceProviders method.


<?php
/**
 * Created by PhpStorm.
 * User: dongyuxiang
 * Date: 2018/7/31
 * Time: 16:53
 */

namespace App\Foundation;

use App\Providers\LogServiceProvider;
use Illuminate\Events\EventServiceProvider;
use Illuminate\Routing\RoutingServiceProvider;
use Illuminate\Foundation\Application as BaseApplication;


class Application extends BaseApplication
{

 /**
  * Register all of the base service providers.
  *
  * @return void
  */
 protected function registerBaseServiceProviders()
 {
  $this->register(new EventServiceProvider($this));

  $this->register(new LogServiceProvider($this));

  $this->register(new RoutingServiceProvider($this));
 }
}

To say rewrite is to change the use class from the LogServiceProvider we created ourselves.

Then in bootstrap\ app.php, replace the new object of the variable $app with the one we inherited and overridden.


$app = new App\Foundation\Application(
 realpath(__DIR__.'/../')
);

In this way, I can successfully define the log path at will, and with this experience, I can make a further step of optimization to meet my requirements where the framework does not meet my requirements, and I have not changed the underlying code of the framework, and I can rest assured to update the framework when the framework has bug fixes.

Summarize


Related articles: