繁体   English   中英

如何正确使用Laravel 4 Profiler

[英]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是完全不同的实体。

如果尚未安装外观和/或服务提供商,请执行以下操作:

  1. 首先,您必须使用composer安装该软件包,确保在“ require”中的composer.json中添加了composer update之后,您已经运行了composer update。-您已经完成了此操作。
  2. 接下来,将'Profiler\\ProfilerServiceProvider',添加到app / config / app.php中的服务提供商列表中
  3. 接下来,将'Profiler' => 'Profiler\\Facades\\Profiler',到app / config / app.php中的类别名列表中
  4. 然后在控制台中运行php artisan config:publish loic-sharma/profiler

完成后,上面修改的代码应该可以正常工作。

为了阐明您做错了什么,您使用new Profiler\\Logger\\Logger;创建了新的new Profiler\\Logger\\Logger;实例new Profiler\\Logger\\Logger; 如果您已经设置好外观,则探查器栏将已经显示(回显)到浏览器,因此当您echo $profiler; 您现在在浏览器中有两个探查器,导致打开关闭问题,并且当您不echo $profiler ,该条仍然显示,因为它不是您创建的,因此无法正确显示输出。

如果仍然要使用自己的探查器实例:

  1. 从app / config / app.php中的服务提供者列表中删除'Profiler\\ProfilerServiceProvider',
  2. 从app / config / app.php中的类别名列表中删除'Profiler' => 'Profiler\\Facades\\Profiler',
  3. 然后在控制台中运行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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM