前言
在 Laravel 5.6 版本中日志行為可以很容易的進(jìn)行自定義,而在5.5以下版本中日志行為自定義自由度并不是很高,但是項(xiàng)目有需求不能因?yàn)檫@個就強(qiáng)行將項(xiàng)目升級為5.6吧,況且作為一個穩(wěn)定的項(xiàng)目升級框架大版本有可能會有很多坑,基于這些原因我嘗試了對 Laravel 5.5 的日志進(jìn)行改造以適應(yīng)我的需求。
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() ); }
這是我最常在項(xiàng)目中使用的日志存儲方式,可以看到日志的存儲路徑幾近與寫死的狀態(tài),無法通過外部參數(shù)輕易的更改。
最開始我想的是重寫這個 Provider 然后將其注冊到 app.php 的 providers 數(shù)組中,但是這種行為并不可行,因?yàn)橥ㄟ^查看源碼, LogServiceProvider 是在框架啟動時就注冊。
在 中有這樣一個方法控制了這個注冊行為:
protected function registerBaseServiceProviders() { $this->register(new EventServiceProvider($this)); $this->register(new LogServiceProvider($this)); $this->register(new RoutingServiceProvider($this)); }
既然我們知道了它們是如何生效的,那么我們將這兩個類繼承并修改其中我們需要改變的行為進(jìn)行改造,我的改造方式如下。在 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)); } }
說是重寫其實(shí)只是將use類換從了我們自己創(chuàng)建的 LogServiceProvider 。
然后在 bootstrap\app.php 中將變量 $app 的 new 對象換成我們繼承重寫后的。
$app = new App\Foundation\Application( realpath(__DIR__.'/../') );
這樣我就成功的將日志路徑可以隨便定義了,而且來說有了這次經(jīng)驗(yàn)我對于框架不符合我需求的地方可以做更進(jìn)一步的優(yōu)化以符合我的要求,而且我沒有更改框架底層的代碼,當(dāng)框架有bug修復(fù)的時候我也可以放心的進(jìn)行框架更新。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
標(biāo)簽:辛集 太原 衡陽 崇左 鄂州 白銀 廊坊 綏化
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel5.5以下版本中如何自定義日志行為詳解》,本文關(guān)鍵詞 Laravel5.5,以下,版本,中,如何,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。