我正在尝试将requestedRoute
,controller name
,action name
logged in user id
等详细信息存储在我的日志表中。
所以我延长了yii\log\DbTarget
。
namespace api\components;
use Yii;
use yii\log\DbTarget;
class DbExpTarget extends DbTarget{
public function export(){
$tableName = $this->db->quoteTableName($this->logTable);
$sql = "INSERT INTO $tableName ([[event]], [[controller]], [[action]], [[userId]], [[createdAt]])
VALUES (:event, :controller, :action,:userId, :createdAt)";
$command = $this->db->createCommand($sql);
$event=Yii::$app->controller->controllerule->requestedRoute;
$controller=Yii::$app->controller->uniqueId;
$action=Yii::$app->controller->action->id;
$user=Yii::$app->user->identity->id;
$command->bindValues([
':event' => $event,
':controller' => $controller,
':action' => $action,
':userId' => $user,
':createdAt' => time(),
])->execute();
}
}
配置/ main.php
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'api\components\DbExpTarget',
'levels' => ['info','trace','error']
]
]
]
但在export()
功能中,我无法访问当前操作($action=Yii::$app->controller->action->id
)并登录用户ID(Yii::$app->user->identity->id
)
我怎样才能获得这些细节?还是记录这些细节的简单方法?