Laravel 重寫日志,讓日志更優(yōu)雅
更改目的:
- 重寫了日志格式
- 加入
trace
,一次請(qǐng)求的唯一標(biāo)識(shí) - 加入
error
級(jí)別信息推送,事例中使用企業(yè)微信群助手 - 讓我們可以更及時(shí)、更優(yōu)雅、更方便追蹤日志信息
- 有助于初學(xué)者了解Laravel框架
1。將文件 AppTool.php
、Logger.php
、LogServiceProvider.php
復(fù)制到 app/Providers
文件夾下,將文件BaseCommand.php
復(fù)制到App\Console
下
2 。在config/app.php→providers
中加入
'providers' => [ …… // 注冊(cè)日志 App\Providers\LogServiceProvider::class …… ];
3。在項(xiàng)目中使用如下方式調(diào)用
// php-fpm方式調(diào)用 日志路徑 /opt/logs/xxx.log /opt/logs/xxx.error \Log::info("info"); \Log::debug("debug"); \Log::error("error"); // 在cli方式調(diào)用 日志路徑 /opt/clogs/xxx.log /opt/clogs/xxx.error app('cLog')->info("info"); app('cLog')->debug("debug"); app('cLog')->error("error");
4。在日志級(jí)別為error
時(shí),會(huì)執(zhí)行推送,本事例中采用企業(yè)微信群推送
/** * 推送錯(cuò)誤信息 * @param $message */ public function pushErrorMessage($message) { $content = "app:". static::getAppName() ." src: ". static::getRequestSource() ." trace:". self::getTrace() ." url:". static::$uri_info ." error: ". $message ." time:". date("Y-m-d H:i:s"); // 測(cè)試群 $url = "xxxxxxxxxxxx"; $result = app('\GuzzleHttp\Client')->request('POST', $url, [ \GuzzleHttp\RequestOptions::JSON=>[ "msgtype"=> "text", "text"=> [ "content" => $content ] ] ]); $body = \GuzzleHttp\json_decode($result->getBody()->getContents(), true); }
5 。日志內(nèi)容
注意事項(xiàng):
修改如下代碼不同版本bind部分會(huì)有所不同,具體根據(jù)\Illuminate\Foundation\Application::registerCoreContainerAliases
中log
信息修改。
如laravel6.x中為'log' => [\Illuminate\Log\LogManager::class, \Psr\Log\LoggerInterface::class],
,
修改方式就如下方代碼
…… // 注入全局容器 $app->instance('Log', $logger); $app->bind('Psr\Log\LoggerInterface', function (Application $app) { return $app['log']->getLogger(); }); $app->bind('\Illuminate\Log\LogManager', function (Application $app) { return $app['log']; }); ……
\Illuminate\Console\Command::info
、\Illuminate\Console\Command::line
、\Illuminate\Console\Command::error
,然后所有console繼承BaseCommanduse App\Console\BaseCommand; class Demo extends BaseCommand { protected $signature = 'command:demo'; protected $description = 'demo'; public function __construct() { parent::__construct(); } public function handle() { $this->info('this is info!'); $this->line('this is line!'); $this->error('this is error!!!'); } }
demo 命令行輸出:
到此這篇關(guān)于Laravel 重寫日志,讓日志更優(yōu)雅的文章就介紹到這了,更多相關(guān)Laravel 重寫日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
thinkphp實(shí)現(xiàn)上一篇與下一篇的方法
這篇文章主要介紹了thinkphp實(shí)現(xiàn)上一篇與下一篇的方法,以實(shí)例形式對(duì)比分析了兩種不同的實(shí)現(xiàn)方法供大家選擇,方法二在方法一的基礎(chǔ)上增加了判斷的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12PHP isset empty函數(shù)相關(guān)面試題及解析
這篇文章主要介紹了PHP isset empty函數(shù)相關(guān)面試題及解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12TP框架實(shí)現(xiàn)上傳一張圖片和批量上傳圖片的方法分析
這篇文章主要介紹了TP框架實(shí)現(xiàn)上傳一張圖片和批量上傳圖片的方法,結(jié)合實(shí)例形式分析了TP框架圖片上傳操作相關(guān)原理、實(shí)現(xiàn)步驟及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04