laravel中的錯誤與日誌用法詳解_php執行個體

來源:互聯網
上載者:User
本文執行個體講述了laravel中的錯誤與日誌用法。分享給大家供大家參考,具體如下:

日誌

laravel中的日誌是基於monolog而封裝的。laravel在它上面做了幾個事情:

① 把monolog中的addInfo等函數簡化成為了info這樣的函數

② 增加了useFiles和useDailyFiles兩個參數,使得做日誌管理和切割變的容易了

③ 如果要調用monolog的方法需要調用callMonolog函數

好了,看下下面幾個需求怎麼實現:

將不同的日誌資訊存放到不同的日誌中去

這個需求很普遍的,比如調用訂單的日誌,需要記錄到order.log,擷取店鋪資訊的記錄需要記錄到shop.log中去。可以這麼做:

<?php use Monolog\Logger;use Monolog\Handler\StreamHandler;use Illuminate\Log\Writer;class BLogger{  // 所有的LOG都要求在這裡註冊  const LOG_ERROR = 'error';  private static $loggers = array();  // 擷取一個執行個體  public static function getLogger($type = self::LOG_ERROR, $day = 30)  {    if (empty(self::$loggers[$type])) {      self::$loggers[$type] = new Writer(new Logger($type));      self::$loggers[$type]->useDailyFiles(storage_path().'/logs/'. $type .'.log', $day);    }    $log = self::$loggers[$type];    return $log;  }}

這樣不同的日誌資料會被儲存到不同的記錄檔中去。還能記錄日誌資料資訊。

laravel的錯誤記錄檔堆棧太長了,怎麼辦?

使用上面的BLogger類,在start/global.php記錄下必要的錯誤資訊

// 錯誤記錄檔資訊App::error(function(Exception $exception, $code){  Log::error($exception);  $err = [    'message' => $exception->getMessage(),    'file' => $exception->getFile(),    'line' => $exception->getLine(),    'code' => $exception->getCode(),    'url' => Request::url(),    'input' => Input::all(),  ];  BLogger::getLogger(BLogger::LOG_ERROR)->error($err);});

laravel預設的日誌沒有使用分割

所以應該預設把laravel的預設日誌記錄改成有分割的。

同樣在start/global.php中

Log::useDailyFiles(storage_path().'/logs/laravel.log', 30);

如何記錄一個請求的sql日誌

這個應該再細化問,你是不是要即時記錄?

如果不要即時記錄,那麼laravel有個DB::getQueryLog可以擷取一個app請求擷取出來的sql請求:

## 在filters.php中App::after(function($request, $response){  // 資料庫查詢進行日誌  $queries = DB::getQueryLog();  if (Config::get('query.log', false)) {    BLogger::getLogger('query')->info($queries);  }}

如果你是需要即時記錄的(也就是你在任何地方die出來的時候,之前的頁面的sql請求也有記錄)的話,你就需要監聽illuminate.query事件了

// 資料庫即時請求的日誌if (Config::get('database.log', false)){  Event::listen('illuminate.query', function($query, $bindings, $time, $name)  {    $data = compact('query','bindings', 'time', 'name');    BLogger::getLogger(BLogger::LOG_QUERY_REAL_TIME)->info($data);  });}

錯誤

laravel的所有錯誤會全部過global的App::error再出來

所以比如你設計的是介面,希望即使有error出現也返回json資料,則可以這麼做:

// 錯誤記錄檔資訊App::error(function(Exception $exception, $code){  // 如果沒有路徑就直接跳轉到登入頁面  if ($exception instanceof NotFoundHttpException) {    return Redirect::route('login');  }  Log::error($exception);  $err = [    'message' => $exception->getMessage(),    'file' => $exception->getFile(),    'line' => $exception->getLine(),    'code' => $exception->getCode(),    'url' => Request::url(),    'input' => Input::all(),  ];  BLogger::getLogger(BLogger::LOG_ERROR)->error($err);  $response = [    'status' => 0,    'error' => "伺服器內部錯誤",  ];  return Response::json($response);});

如果你還希望將404錯誤也hold住:

App::missing(function($exception){  $response = [    'status' => 0,    'error' => "請求路徑錯誤",  ];  return Response::json($response);});

更多關於Laravel相關內容感興趣的讀者可查看本站專題:《Laravel架構入門與進階教程》、《php優秀開發架構總結》、《smarty模板入門基礎教程》、《php日期與時間用法總結》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於Laravel架構的PHP程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.