In Laravel 5.6, the log behavior can be easily customized, while in Laravel 5.5 and below, the freedom of log behavior customization is not very high, but the project needs not be forced to upgrade to 5.6 because of this, and as a stable project upgrade framework, there may be many bugs in the big version. For these reasons I tried adapting the Laravel 5.5 log to suit my needs.

Laravel’s logging behavior is mostly in Illuminate\Log\LogServiceProvider, which we can look at ina code snippet:

/**
 * 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()
    );
}
Copy the code

This is the log storage method I use most often in my projects. You can see that the log storage path is almost dead and cannot be easily changed by external parameters.

My initial thought was to rewrite the Provider and register it in the providers array of app.php, but this is not feasible because, looking at the source code, LogServiceProvider is registered when the framework starts.

There is a method that controls this registration behavior in:

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

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

    $this->register(new RoutingServiceProvider($this));
}
Copy the code

Now that we know how they work, we inherit these two classes and modify the behaviors we need to change. Here’s how I modify them. Create a new LogServiceProvider class in app\Providers that inherits Illuminate\Log\LogServiceProvider as follows:


      


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() ); }}Copy the code

Add config to the config/app.php directory:

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

App directory Foundation new directory, new Application class inheritance Illuminate, Foundation, Application class, rewrite registerBaseServiceProviders method.


      
/** * 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(a)
    {
        $this->register(new EventServiceProvider($this));

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

        $this->register(new RoutingServiceProvider($this)); }}Copy the code

The rewrite is really just replacing the Use class with the LogServiceProvider we created ourselves.

Then in bootstrap\app.php, change the new object of the variable $app to our inherited override.

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

So I will log path can literally defines success, and with the experience I for framework does not conform to the place where I need to do further optimization to meet my requirements, and I didn’t change the framework of the underlying code, when the frame has a bug fix frame updates I also can rest assured.