如何在laravel中使用錯誤與日志?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

日志
laravel中的日志是基于monolog而封裝的。laravel在它上面做了幾個事情:
① 把monolog中的addInfo等函數(shù)簡化成為了info這樣的函數(shù)
② 增加了useFiles和useDailyFiles兩個參數(shù),使得做日志管理和切割變的容易了
③ 如果要調(diào)用monolog的方法需要調(diào)用callMonolog函數(shù)
好了,看下下面幾個需求怎么實現(xiàn):
將不同的日志信息存放到不同的日志中去
這個需求很普遍的,比如調(diào)用訂單的日志,需要記錄到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;
}
}這樣不同的日志數(shù)據(jù)會被存儲到不同的日志文件中去。還能記錄日志數(shù)據(jù)信息。
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默認的日志沒有使用分割
所以應(yīng)該默認把laravel的默認日志記錄改成有分割的。
同樣在start/global.php中
Log::useDailyFiles(storage_path().'/logs/laravel.log', 30);
如何記錄一個請求的sql日志
這個應(yīng)該再細化問,你是不是要實時記錄?
如果不要實時記錄,那么laravel有個DB::getQueryLog可以獲取一個app請求獲取出來的sql請求:
## 在filters.php中
App::after(function($request, $response)
{
// 數(shù)據(jù)庫查詢進行日志
$queries = DB::getQueryLog();
if (Config::get('query.log', false)) {
BLogger::getLogger('query')->info($queries);
}
}如果你是需要實時記錄的(也就是你在任何地方die出來的時候,之前的頁面的sql請求也有記錄)的話,你就需要監(jiān)聽illuminate.query事件了
// 數(shù)據(jù)庫實時請求的日志
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再出來
所以比如你設(shè)計的是接口,希望即使有error出現(xiàn)也返回json數(shù)據(jù),則可以這么做:
// 錯誤日志信息
App::error(function(Exception $exception, $code)
{
// 如果沒有路徑就直接跳轉(zhuǎn)到登錄頁面
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' => "服務(wù)器內(nèi)部錯誤",
];
return Response::json($response);
});如果你還希望將404錯誤也hold住:
App::missing(function($exception)
{
$response = [
'status' => 0,
'error' => "請求路徑錯誤",
];
return Response::json($response);
});看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
當(dāng)前題目:如何在laravel中使用錯誤與日志-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://www.chinadenli.net/article48/djophp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計公司、品牌網(wǎng)站制作、網(wǎng)站內(nèi)鏈、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容