Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created July 22, 2013 16:17
Show Gist options
  • Save mindplay-dk/6055198 to your computer and use it in GitHub Desktop.
Save mindplay-dk/6055198 to your computer and use it in GitHub Desktop.
Basic integration for ChromeLogger with Yii

ChromeLogger integration for Yii

Basic implementation of a CLogRoute that routes to (ChromeLogger)[https://github.com/ccampbell/chromelogger].

Grab a copy of (ChromePhp.php)[https://github.com/ccampbell/chromephp] and drop into {application root}/vendor/chromephp along with ChromePhpLogRoute.php below.

Configure as follows:

'log'=>array(
    'class'  => 'CLogRouter',
    'routes' => array(
        // ...
        'chrome' => array(
            'class'      => 'application.vendor.chromephp.ChromePhpLogRoute',
            'levels'     => 'trace, error, warning, info',
            'enabled'    => true,
            'categories' => 'application.*, system.db.CDbCommand',
        ),
    ),
),

'db' => array(
    'enableProfiling' => true,
    'enableParamLogging' => true,
    // ...
),

Install and enable ChromeLogger, and you should see DB queries etc. on your Chrome console, even during AJAX requests...

<?php
Yii::import('application.vendor.chromephp.ChromePhp');
class ChromePhpLogRoute extends CLogRoute
{
/**
* @var bool whether the log should be ignored for ajax calls. Defaults to true.
*/
public $ignoreAjax = false;
/**
* @var bool whether the log should be ignored for Flash/Flex calls. Defaults to true.
*/
public $ignoreFlash = false;
/**
* @var bool whether the log should display as initially collapsed in the Chrome console. Defaults to false.
*/
public $collapsed = false;
const TITLE = 'Yii Application Log';
/**
* Displays the log messages.
* @param array $logs list of log messages
*/
public function processLogs($logs)
{
$app = Yii::app();
$isAjax = $app->getRequest()->getIsAjaxRequest();
$isFlash = $app->getRequest()->getIsFlashRequest();
//ChromePhp::log($logs); return;
if ($isAjax && $this->ignoreAjax || $isFlash && $this->ignoreFlash) {
return; // AJAX and/or Flash ignored
}
if ($this->collapsed) {
ChromePhp::groupCollapsedgroup(self::TITLE);
} else {
ChromePhp::group(self::TITLE);
}
foreach ($logs as $log) {
switch ($log[1]) {
case CLogger::LEVEL_WARNING:
$method = 'warn';
break;
case CLogger::LEVEL_ERROR:
$method = 'error';
break;
case CLogger::LEVEL_INFO:
$method = 'info';
break;
case CLogger::LEVEL_PROFILE:
continue; // TODO
case CLogger::LEVEL_TRACE:
continue; // TODO
default:
$method = 'log';
}
$time = date('H:i:s.', $log[3])
. sprintf('%03d', (int)(($log[3] - (int)$log[3]) * 1000));
$data = "[{$time}][{$log[1]}][{$log[2]}] {$log[0]}";
call_user_func_array(array('ChromePhp', $method), array($data));
}
ChromePhp::groupEnd();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment