How to customize the analysis of log behavior in Laravel5.5
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() ); }
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)); }
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('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')),
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)); } }
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__.'/../') );
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

How to check the validity of Redis connections in Laravel6 projects is a common problem, especially when projects rely on Redis for business processing. The following is...

A problem of duplicate class definition during Laravel database migration occurs. When using the Laravel framework for database migration, developers may encounter "classes have been used...
