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

Aug 02, 2018 pm 05:32 PM
laravel log

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

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...

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

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

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

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

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

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...

Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

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->

Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

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 effectively check the validity of Redis connections in Laravel6 project? How to effectively check the validity of Redis connections in Laravel6 project? Apr 01, 2025 pm 02:00 PM

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...

Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Apr 01, 2025 pm 12:21 PM

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...

See all articles