Yii 內建日誌功能強大,但預設輸出至檔案,不便即時查看。yiidebugtb 提供網頁工具列,顯示 SQL、變數、效能分析,且不干擾頁面佈局。
yiidebugtb,解壓至 protected/extensions/yiidebugtb。protected/config/main.php,調整 log 元件:'log' => array(
'class' => 'CLogRouter',
'routes' => array(
array(
'class' => 'CFileLogRoute',
'levels' => 'error, warning, trace',
),
// 新增工具列路由
array(
'class' => 'XWebDebugRouter',
'config' => 'alignLeft, opaque, runInDebug, fixedPos, collapsed, yamlStyle',
'levels' => 'error, warning, trace, profile, info',
// 'categories' => 'system.db.*', // 只看 DB 相關可取消註解
'allowedIPs' => array('127.0.0.1', '::1', '192.168.1.[0-5].[0-9]{3}', '你的公網IP'),
),
),
),
'db' => array(
'connectionString' => 'mysql:host=...;dbname=test',
'emulatePrepare' => true, // 必須:啟用參數記錄
'enableParamLogging' => true, // 必須:記錄綁定參數值
'username' => '...',
'password' => '...',
'charset' => 'utf8',
// ...
),
allowedIPs、YII_DEBUG 為 true、runInDebug 選項已開啟。enableParamLogging 為 true,且使用 CDbCommand 或 ActiveRecord 執行查詢。Yii::log($sql, 'trace', 'system.db.CDbCommand');
可將手寫 SQL 也納入工具列顯示。