《PHP實例:laravel中的錯誤與日志用法詳解》要點:
本文介紹了PHP實例:laravel中的錯誤與日志用法詳解,希望對您有用。如果有疑問,可以聯系我們。
本文實例講述了laravel中的錯誤與日志用法.分享給大家供大家參考,具體如下:PHP編程
日志PHP編程
laravel中的日志是基于monolog而封裝的.laravel在它上面做了幾個事情:PHP編程
① 把monolog中的addInfo等函數簡化成為了info這樣的函數PHP編程
② 增加了useFiles和useDailyFiles兩個參數,使得做日志管理和切割變的容易了PHP編程
③ 如果要調用monolog的方法需要調用callMonolog函數PHP編程
好了,看下下面幾個需求怎么實現:PHP編程
將不同的日志信息存放到不同的日志中去PHP編程
這個需求很普遍的,比如調用訂單的日志,需要記錄到order.log,獲取店鋪信息的記錄需要記錄到shop.log中去.可以這么做:PHP編程
<?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; } }
這樣不同的日志數據會被存儲到不同的日志文件中去.還能記錄日志數據信息.PHP編程
laravel的錯誤日志堆棧太長了,怎么辦?PHP編程
使用上面的BLogger類,在start/global.php記錄下必要的錯誤信息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默認的日志沒有使用分割PHP編程
所以應該默認把laravel的默認日志記錄改成有分割的.PHP編程
同樣在start/global.php中PHP編程
Log::useDailyFiles(storage_path().'/logs/laravel.log', 30);
如何記錄一個請求的sql日志PHP編程
這個應該再細化問,你是不是要實時記錄?PHP編程
如果不要實時記錄,那么laravel有個DB::getQueryLog可以獲取一個app請求獲取出來的sql請求:PHP編程
## 在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事件了PHP編程
// 數據庫實時請求的日志 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); }); }
錯誤PHP編程
laravel的所有錯誤會全部過global的App::error再出來PHP編程
所以比如你設計的是接口,希望即使有error出現也返回json數據,則可以這么做:PHP編程
// 錯誤日志信息 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住:PHP編程
App::missing(function($exception) { $response = [ 'status' => 0, 'error' => "請求路徑錯誤", ]; return Response::json($response); });
更多關于Laravel相關內容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優秀開發框架總結》、《smarty模板入門基礎教程》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》PHP編程
希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助.PHP編程
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/5066.html