Yii::error() ? 記錄一個致命錯誤訊息
Yii::warning() ? 記錄一個警告訊息
Yii::info() ? 記錄一些有用資訊的訊息
Yii::trace() ? 記錄訊息跟蹤一段程式碼如何執行
function ($message, $category = 'application')
在這裡 ?
$message ? 要記錄的紀錄檔訊息
$category ? 紀錄檔訊息類別
Yii::info('this is a log message', __METHOD__);
return [ // the "log" component is loaded during bootstrapping time 'bootstrap' => ['log'], 'components' => [ 'log' => [ 'targets' => [ [ 'class' => 'yii\log\DbTarget', 'levels' => ['error', 'warning', 'trace', 'info'], ], [ 'class' => 'yii\log\EmailTarget', 'levels' => ['error', 'warning'], 'categories' => ['yii\db\*'], 'message' => [ 'from' => ['[email protected]'], 'to' => ['[email protected]', '[email protected]'], 'subject' => 'Application errors at mydomain.com', ], ], ], ], ], ];
yii\log\DbTarget ? 在資料庫中儲存紀錄檔訊息
yii\log\FileTarget ? 在檔案中儲存記錄訊息
yii\log\EmailTarget ? 傳送長訊息到預先定義的電子郵件地址
yii\log\SyslogTarget ? 通過呼叫 PHP syslog()函式將紀錄檔資訊儲存到系統紀錄檔
Timestamp [IP address][User ID][Session ID][Severity Level][Category] Message Text
[ 'class' => 'yii\log\FileTarget', 'prefix' => function ($message) { $user = Yii::$app->has('user', true) ? Yii::$app->get('user') : 'undefined user'; $userID = $user ? $user->getId(false) : 'anonym'; return "[$userID]"; } ]
return [ 'bootstrap' => ['log'], 'components' => [ 'log' => [ 'flushInterval' => 50, // default is 1000 'targets' => [...], ], ], ];
[ 'class' => 'yii\log\FileTarget', 'exportInterval' => 50, // default is 1000 ]
<?php $params = require(__DIR__ . '/params.php'); $config = [ 'id' => 'basic', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'components' => [ 'request' => [ // !!! insert a secret key in the following (if it is empty) - this //is required by cookie validation 'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO', ], 'cache' => [ 'class' => 'yii\caching\FileCache', ], 'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true, ], 'errorHandler' => [ 'errorAction' => 'site/error', ], 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', // send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => true, ], 'log' => [ 'flushInterval' => 1, 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'exportInterval' => 1, 'logVars' => [] ], ], ], 'db' => require(__DIR__ . '/db.php'), ], 'modules' => [ 'admin' => [ 'class' => 'app\modules\admin\Admin', ], ], 'params' => $params, ]; if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = [ 'class' => 'yii\debug\Module', ]; $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class' => 'yii\gii\Module', ]; } return $config; ?>
public function actionLog() { Yii::trace('trace log message'); Yii::info('info log message'); Yii::warning('warning log message'); Yii::error('error log message'); }