shiki/yii-firephp

Yii 框架的 FirePHP 扩展。

0.3.0 2013-11-15 21:58 UTC

This package is not auto-updated.

Last update: 2024-09-24 00:52:59 UTC


README

此扩展包含2个日志路由类。第一个是 SK\Yii\FirePHP\LogRoute,处理标准的 Yii 日志消息。第二个是 SK\Yii\FirePHP\ProfileLogRoute,处理性能摘要。这两个类都将所有输出发送到 FirePHP。这些类与 CWebLogRouteCProfileLogRoute 类似。唯一的主要区别是目标输出。

使用此扩展的优势在于,日志和性能分析即使在 AJAX 请求中也能正常工作。

要求

  • PHP 5.4+
  • Yii 框架 1.1.14+ 项目。
  • Firefox 的 Firebug 和 FirePHP 插件。请参阅 http://firephp.org。要使此功能正常工作,必须启用 Firebug 的 ConsoleNet 选项卡。
  • php.ini 中将 output_buffering 设置为 true。您可能还需要增加缓冲区大小,以允许大日志大小。

安装

目前唯一支持的安装方法是使用 Composer

  1. 将以下内容放入您的 composer.json 中,并运行 composer update 以安装它
{
  "require": {
    "shiki/yii-firephp": "dev-master"
  }
}

这将自动安装依赖项 firephp/firephp-core

  1. 请确保您已加载 Composer 自动加载文件(vendor/autoload.php),以便在 Yii 配置文件中访问库。有关如何操作的示例,请参阅 example 项目的 main.php 配置文件。

  2. 修改您的配置文件(例如 protected/config/main.php),以包含日志路由类。

....

'log' => array(
  'class' => 'CLogRouter',
  'routes' => array(
    // the default (file logger)
    array(
      'class' => 'CFileLogRoute',
      'levels' => 'error, warning',
    ),
    // standard log route
    array(
      'class' => '\\SK\\Yii\\FirePHP\\LogRoute',
      'levels' => 'error, warning, info, trace',
    ),
    // profile log route
    array(
      'class' => '\\SK\\Yii\\FirePHP\\ProfileLogRoute',
      'report' => 'summary', // or "callstack"
    ),
  ),
),

....

标准日志记录

一旦您在配置中设置了扩展,您就可以使用 Yii 的日志记录方法将消息记录到 FirePHP。

// logging an INFO message
Yii::log('This is an info message.', CLogger::LEVEL_INFO);

// logging a WARNING message
Yii::log("You didn't setup a profile, are you really a person?", CLogger::LEVEL_WARNING);

// logging with a CATEGORY (categories are displayed as "labels" in FirePHP -- just an additional info text)
Yii::log('Profile successfully created', CLogger::LEVEL_INFO, 'application.user.profiles');

// tracing simple text
Yii::trace('Loading application.user.profiles.ninja', 'application.user.profiles');

// logging an ERROR
Yii::log('We have successfully determined that you are not a person',
  CLogger::LEVEL_ERROR, 'Any category/label will work');

// If you need to log an array, you can use FirePHP's core methods
FB::warn(array('a' => 'b', 'c' => 'd'), 'an.array.warning');

有关日志记录的更多信息,请参阅 此处

性能分析

性能分析通过简单地使用 Yii 的性能分析方法实现。

Yii::beginProfile('a somewhat slow method');

...
// some function calls here
// more function calls

Yii::beginProfile('nested profile');
// you can also nest profile calls
Yii::endProfile('nested profile');

Yii::endProfile('a somewhat slow method'); // end

您还可以对 SQL 执行进行性能分析。有关此信息和性能分析的一般信息,请参阅 此处

示例

要尝试所有这些,example 文件夹中有一个示例项目。要运行它

  1. 使用 Composer 安装所需的库。

     $ cd example
     $ composer install
    
  2. 使用 PHP 内置的 Web 服务器运行。

     $ cd example/webroot
     $ php -S localhost:8000
    
  3. 在 Firefox 中浏览 https://:8000。请确保首先打开 Firebug,并启用 Console 和 Net 选项卡。您应该在 Firebug 的控制台中看到 FirePHP 日志。如果看不到,请先刷新页面。