下面由laravel教程欄目給大家介紹laravel中的錯誤與日志(可以自定義日志目錄和log文件名),希望對需要的朋友有所幫助!
日志
laravel中的日志是基于monolog而封裝的。laravel在它上面做了幾個事情:
- 把monolog中的addInfo等函數簡化成為了info這樣的函數
- 增加了useFiles和useDailyFiles兩個參數,使得做日志管理和切割變的容易了
- 如果要調用monolog的方法需要調用callMonolog函數
好了,看下下面幾個需求怎么實現:
將不同的日志信息存放到不同的日志中去
這個需求很普遍的,比如調用訂單的日志,需要記錄到order.log,獲取店鋪信息的記錄需要記錄到shop.log中去。可以這么做:
<?php use MonologLogger; use MonologHandlerStreamHandler; use IlluminateLogWriter; 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); });
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END
喜歡就支持一下吧
相關推薦