編輯config/web.php
首先log要開啟
'bootstrap' => [ 'log' ],
[file]
'components' => [ 'log' => [ 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'exportInterval' => 1, ], ], ],
預設輸出到runtime/logs/app.log
注意webserver或控制台使用者要有寫入該檔案的權限
[database]'log' => [ 'targets' => [ [ 'class' => 'yii\log\DbTarget', 'levels' => ['error', 'warning', 'trace'], ] ] ],預設輸出db元件對應的資料庫下{{%log}}表 yii2根目錄下執行下面指令產生對應的table schema
./yii migrate --migrationPath=@yii/log/migrations/注意config/console.php下也要有和web .php同樣的配置,否則指令執行不成功。 也可以依照不同的環境來設定不同的日誌模式
'components' => [ 'log' => [ 'traceLevel' => YII_ENV == 'dev' ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\DbTarget', 'levels' => YII_DEBUG ? ['error', 'warning', 'trace'] : ['error'], ], [ 'class' => 'yii\log\FileTarget', 'levels' => YII_DEBUG ? ['error', 'warning', 'trace'] : ['error', 'warning'], ], ], ], ],