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

Undress AI Tool
Undress images for free

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

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

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

Middleware is a filtering mechanism in Laravel that is used to intercept and process HTTP requests. Use steps: 1. Create middleware: Use the command "phpartisanmake:middlewareCheckRole". 2. Define processing logic: Write specific logic in the generated file. 3. Register middleware: Add middleware in Kernel.php. 4. Use middleware: Apply middleware in routing definition.

Laravel'sMVCarchitecturecanfaceseveralissues:1)Fatcontrollerscanbeavoidedbydelegatinglogictoservices.2)Overloadedmodelsshouldfocusondataaccess.3)Viewsshouldremainsimple,avoidingPHPlogic.4)PerformanceissueslikeN 1queriescanbemitigatedwitheagerloading.

Laravel's migration is a database version control tool that allows developers to programmatically define and manage database structure changes. 1. Create a migration file using the Artisan command. 2. The migration file contains up and down methods, which defines the creation/modification and rollback of database tables respectively. 3. Use the phpartisanmigrate command to execute the migration, and use phpartisanmigrate:rollback to rollback.

Laravel is suitable for beginners to create MVC projects. 1) Install Laravel: Use composercreate-project--prefer-distlaravel/laravelyour-project-name command. 2) Create models, controllers and views: Define Post models, write PostController processing logic, create index and create views to display and add posts. 3) Set up routing: Configure/posts-related routes in routes/web.php. With these steps, you can build a simple blog application and master the basics of Laravel and MVC.

InLaravel,policiesorganizeauthorizationlogicformodelactions.1.Policiesareclasseswithmethodslikeview,create,update,anddeletethatreturntrueorfalsebasedonuserpermissions.2.Toregisterapolicy,mapthemodeltoitspolicyinthe$policiesarrayofAuthServiceProvider.

In Laravel, routing is the entry point of the application that defines the response logic when a client requests a specific URI. The route maps the URL to the corresponding processing code, which usually contains HTTP methods, URIs, and actions (closures or controller methods). 1. Basic structure of route definition: bind requests using Route::verb('/uri',action); 2. Supports multiple HTTP verbs such as GET, POST, PUT, etc.; 3. Dynamic parameters can be defined through {param} and data can be passed; 4. Routes can be named to generate URLs or redirects; 5. Use grouping functions to uniformly add prefixes, middleware and other sharing settings; 6. Routing files are divided into web.php, ap according to their purpose

Thephpartisandb:seedcommandinLaravelisusedtopopulatethedatabasewithtestordefaultdata.1.Itexecutestherun()methodinseederclasseslocatedin/database/seeders.2.Developerscanrunallseeders,aspecificseederusing--class,ortruncatetablesbeforeseedingwith--trunc

Artisan is a command line tool of Laravel to improve development efficiency. Its core functions include: 1. Generate code structures, such as controllers, models, etc., and automatically create files through make: controller and other commands; 2. Manage database migration and fill, use migrate to run migration, and db:seed to fill data; 3. Support custom commands, such as make:command creation command class to implement business logic encapsulation; 4. Provide debugging and environment management functions, such as key:generate to generate keys, and serve to start the development server. Proficiency in using Artisan can significantly improve Laravel development efficiency.
