欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Laravel 重寫日志,讓日志更優(yōu)雅

 更新時(shí)間:2021年03月19日 11:17:43   作者:凱丨  
這篇文章主要介紹了Laravel 重寫日志,讓日志更優(yōu)雅,laravel框架俗稱優(yōu)雅的框架,所以有想對(duì)laravel中的日志重寫使其更加方便的記錄信息獲取信息的同學(xué)可以參考下

更改目的:

  • 重寫了日志格式
  • 加入trace,一次請(qǐng)求的唯一標(biāo)識(shí)
  • 加入error級(jí)別信息推送,事例中使用企業(yè)微信群助手
  • 讓我們可以更及時(shí)、更優(yōu)雅、更方便追蹤日志信息
  • 有助于初學(xué)者了解Laravel框架

1。將文件 AppTool.phpLogger.phpLogServiceProvider.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::registerCoreContainerAliaseslog信息修改。
如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'];
    });
    ……
有關(guān)console中使用時(shí),建議重寫\Illuminate\Console\Command::info、\Illuminate\Console\Command::line、\Illuminate\Console\Command::error,然后所有console繼承BaseCommand
demo代碼塊:
use 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)文章

最新評(píng)論