[英]How to use Laravel 4 profiler correctly
我正在嘗試提高網站中某些代碼的性能,並找到了此探查器: https : //github.com/loic-sharma/profiler
我遵循了網站上的指導,並在以下網站控制器之一中添加了以下內容:
public function getTest() {
$logger = new Profiler\Logger\Logger;
$profiler = new Profiler\Profiler($logger);
$profiler->startTimer('testLogging');
$data = Article::select(array(
'articles.id',
'articles.article_date',
'articles.image_link',
'articles.headline',
'articles.category'
)) ->get()
->toArray();
var_dump($data);
$profiler->endTimer('testLogging');
Log::info('Hello World!');
echo $profiler;
在瀏覽器中,我得到了預期的結果,並且可以在底部看到探查器欄。
我有一個問題:在此基本測試中,探查器欄沒有保持打開狀態,因此無法查看日志等。我不確定為什么或如何進行修復。 窗格將打開,然后立即再次關閉。
如果我刪除最后的回聲,它將正常工作。
我似乎看不到工具欄中的計時器“ testLogging”。
我在這里誤解了一個概念嗎?
如何在代碼中計時特定功能並顯示結果?
謝謝
要在Laravel 4中正確使用profiler(loic-sharma / profiler),例如,它不需要您創建對象的實例。
$logger = new Profiler\Logger\Logger;
$profiler = new Profiler\Profiler($logger);
Laravel具有稱為Facades( http://laravel.com/docs/facades )的這些漂亮的東西,探查器實現了它們,因此您可以像這樣調用探查器和日志:
public function getTest() {
Profiler::startTimer('testLogging');
$data = Article::select(array(
'articles.id',
'articles.article_date',
'articles.image_link',
'articles.headline',
'articles.category'
)) ->get()
->toArray();
var_dump($data);
Profiler::endTimer('testLogging');
Log::info('Hello World!');
}
這不需要您回顯$ profiler,所有輸出將自動顯示在瀏覽器的探查器欄中。
請注意, ::
現在在Profiler之后通常意味着您正在使用外觀,重要的是要了解外觀和$ profiler是完全不同的實體。
如果尚未安裝外觀和/或服務提供商,請執行以下操作:
'Profiler\\ProfilerServiceProvider',
添加到app / config / app.php中的服務提供商列表中 'Profiler' => 'Profiler\\Facades\\Profiler',
到app / config / app.php中的類別名列表中 php artisan config:publish loic-sharma/profiler
完成后,上面修改的代碼應該可以正常工作。
為了闡明您做錯了什么,您使用new Profiler\\Logger\\Logger;
創建了新的new Profiler\\Logger\\Logger;
實例new Profiler\\Logger\\Logger;
如果您已經設置好外觀,則探查器欄將已經顯示(回顯)到瀏覽器,因此當您echo $profiler;
您現在在瀏覽器中有兩個探查器,導致打開關閉問題,並且當您不echo $profiler
,該條仍然顯示,因為它不是您創建的,因此無法正確顯示輸出。
如果仍然要使用自己的探查器實例:
'Profiler\\ProfilerServiceProvider',
'Profiler' => 'Profiler\\Facades\\Profiler',
php artisan dump-autoload
然后這將起作用:
public function getTest() {
$logger = new Profiler\Logger\Logger;
$profiler = new Profiler\Profiler($logger);
$profiler->startTimer('testLogging');
$data = Article::select(array(
'articles.id',
'articles.article_date',
'articles.image_link',
'articles.headline',
'articles.category'
)) ->get()
->toArray();
$logger->debug(var_dump($data));
$profiler->endTimer('testLogging');
$logger->info('Hello World!');
echo $profiler;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.