Laravel5.5以下版本中自定义日志行为的示例

发布时间:2021-02-08 10:23:14 作者:小新
来源:亿速云 阅读:146

小编给大家分享一下Laravel5.5以下版本中自定义日志行为的示例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

前言

在 Laravel 5.6 版本中日志行为可以很容易的进行自定义,而在5.5以下版本中日志行为自定义自由度并不是很高,但是项目有需求不能因为这个就强行将项目升级为5.6吧,况且作为一个稳定的项目升级框架大版本有可能会有很多坑,基于这些原因我尝试了对 Laravel 5.5 的日志进行改造以适应我的需求。

Laravel 的日志行为大部分是在 Illuminate\Log\LogServiceProvider 中,我们可以看一下其中的代码片段:

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

这是我最常在项目中使用的日志存储方式,可以看到日志的存储路径几近与写死的状态,无法通过外部参数轻易的更改。

最开始我想的是重写这个 Provider 然后将其注册到 app.php 的 providers 数组中,但是这种行为并不可行,因为通过查看源码, LogServiceProvider 是在框架启动时就注册。

在 中有这样一个方法控制了这个注册行为:

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

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

 $this->register(new RoutingServiceProvider($this));
}

既然我们知道了它们是如何生效的,那么我们将这两个类继承并修改其中我们需要改变的行为进行改造,我的改造方式如下。在 app\Providers 中新建 LogServiceProvider 类继承 Illuminate\Log\LogServiceProvider ,代码如下:

<?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()
  );
 }
}

在 config/app.php 目录中添加配置:

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

app 目录中新建 Foundation 目录,新建 Application 类继承 Illuminate\Foundation\Application 类,重写 registerBaseServiceProviders 方法。

<?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));
 }
}

说是重写其实只是将use类换从了我们自己创建的 LogServiceProvider 。

然后在 bootstrap\app.php 中将变量 $app 的 new 对象换成我们继承重写后的。

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

这样我就成功的将日志路径可以随便定义了,而且来说有了这次经验我对于框架不符合我需求的地方可以做更进一步的优化以符合我的要求,而且我没有更改框架底层的代码,当框架有bug修复的时候我也可以放心的进行框架更新。

以上是“Laravel5.5以下版本中自定义日志行为的示例”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. openldap的搭建(版本在2.4以下)
  2. XML行为behavior的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

laravel5.5

上一篇:mpvue怎么使用腾讯视频插件的方法

下一篇:Asp.Net Core控制器怎么接收原始请求正文内容

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》