佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > Laravel 重寫日志,讓日志更優(yōu)雅

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

熱門標(biāo)簽:電子圍欄 銀行業(yè)務(wù) 阿里云 Linux服務(wù)器 服務(wù)器配置 團(tuán)購網(wǎng)站 科大訊飛語音識(shí)別系統(tǒng) Mysql連接數(shù)設(shè)置

更改目的:

  • 重寫了日志格式
  • 加入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");
    // 測試群
    $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)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Laravel的加密解密與哈希實(shí)例講解
  • Laravel 數(shù)據(jù)庫加密及數(shù)據(jù)庫表前綴配置方法
  • laravel 5.3中自定義加密服務(wù)的方案詳解
  • Laravel中10個(gè)有用的用法小結(jié)
  • Laravel中的where高級(jí)使用方法實(shí)例講解
  • laravel使用組件實(shí)現(xiàn)微信網(wǎng)頁授權(quán)登入
  • Laravel統(tǒng)一封裝接口返回狀態(tài)實(shí)例講解
  • 淺談Laravel中如何對(duì)大文件進(jìn)行加密

標(biāo)簽:衢州 大理 江蘇 棗莊 廣元 衡水 蚌埠 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel 重寫日志,讓日志更優(yōu)雅》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    清河县| 巴东县| 雅安市| 青浦区| 喜德县| 岑溪市| 呼和浩特市| 河南省| 黄龙县| 鸡西市| 江川县| 东宁县| 六安市| 礼泉县| 星座| 台北县| 社旗县| 乌拉特后旗| 镇雄县| 华安县| 县级市| 商水县| 通化县| 大悟县| 前郭尔| 苏尼特右旗| 抚顺县| 门源| 麻江县| 张掖市| 伊川县| 贵州省| 洮南市| 榆林市| 汽车| 连南| 大新县| 双柏县| 抚远县| 阿尔山市| 江都市|