Home > PHP Framework > Laravel > How to customize the analysis of log behavior in Laravel5.5

How to customize the analysis of log behavior in Laravel5.5

不言
Release: 2018-08-02 17:32:03
Original
3203 people have browsed it

The content of this article is to share with you the analysis of how to customize the log behavior in Laravel5.5. The content is very detailed and has certain reference value. I hope it can help friends in need.

In Laravel 5.6 version, log behavior can be easily customized, while in versions below 5.5, the degree of freedom to customize log behavior is not very high, but if the project has needs, it cannot be forced because of this. Upgrade the project to 5.6. Moreover, as a stable project, upgrading the framework to a large version may have many pitfalls. For these reasons, I tried to transform the logs of Laravel 5.5 to suit my needs.

Most of Laravel's logging behavior is in Illuminate\Log\LogServiceProvider. We can take a look at the code snippets:

/**
 * 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 after login

This is the log storage method I use most often in projects. , you can see that the log storage path is almost hard-coded and cannot be easily changed through external parameters.

At first I thought of rewriting this Provider and registering 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 such a method that controls this registration behavior:

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

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

 $this->register(new RoutingServiceProvider($this));
}
Copy after login

Now that we know how they take effect, we inherit these two classes and modify them We need to change the behavior to transform, and my method of transformation is as follows. Create a new LogServiceProvider class in app\Providers and inherit Illuminate\Log\LogServiceProvider. The code is as follows:

<?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(&#39;app.log_path&#39;);
  $log->useDailyFiles(
   $path, $this->maxFiles(),
   $this->logLevel()
  );
 }
}
Copy after login

Add configuration in the config/app.php directory:

&#39;log_path&#39; => env(&#39;APP_LOG_PATH&#39;, storage_path(&#39;/logs/laravel.log&#39;)),
Copy after login

app Create a new Foundation directory in the directory, create a new Application class that inherits the Illuminate\Foundation\Application class, and overrides 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));
 }
}
Copy after login

It is said that it is rewritten. In fact, it just changes 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 rewritten.

$app = new App\Foundation\Application(
 realpath(__DIR__.&#39;/../&#39;)
);
Copy after login

In this way, I successfully defined the log path casually, and with this experience, I can further optimize the areas where the framework does not meet my needs to meet my requirements, and I have not changed the underlying code of the framework, and I can safely update the framework when there are bug fixes in the framework.

Recommended related articles:

php Detailed explanation of custom error log examples

Yii2 Custom log file writing log

The above is the detailed content of How to customize the analysis of log behavior in Laravel5.5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template