yii2:如何从日志中排除DbTarget信息?

yii2:如何从日志中排除DbTarget信息?,yii2,Yii2,我创建了自己的日志目标,比如DbTarget,以便在记录日志时在数据库中记录一些特定信息,但每个执行查询都由其他目标记录。如何禁用它 目标配置: $except = [ 'yii\db\*', 'yii\filters\RateLimiter::beforeAction', 'yii\web\Session::open', 'yii\swiftmailer\Mailer::sendMessage', 'accessLog', ]; return [

我创建了自己的日志目标,比如DbTarget,以便在记录日志时在数据库中记录一些特定信息,但每个执行查询都由其他目标记录。如何禁用它

目标配置:

$except = [
    'yii\db\*',
    'yii\filters\RateLimiter::beforeAction',
    'yii\web\Session::open',
    'yii\swiftmailer\Mailer::sendMessage',
    'accessLog',
];

return [
    [
        'class'          => 'common\cms\log\AdvDbTarget',
        'levels'         => ['error', 'warning'],
        'exportInterval' => 1,
        'except'         => [
            'yii\db\*',
            'yii\filters\RateLimiter::beforeAction',
            'yii\web\Session::open',
            'yii\swiftmailer\Mailer::sendMessage',
        ],
        'logVars'        => ['_GET', '_POST', '_SERVER', '_COOKIE', '_SESSION'],
        'disableTargets' => ['query'],
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'categories'     => ['yii\swiftmailer\Logger::add'],
        'exportInterval' => 1,
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'except'         => $except,
        'logFile'        => '@logs/application.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
        'logVars'        => [],
        'levels'         => ['info']
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'except'         => $except,
        'levels'         => ['error', 'warning'],
        'logFile'        => '@logs/application.error.' . date('Y-m-d') . '.log',
        'logVars'        => ['_GET', '_POST', '_SERVER'],
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'except'         => $except,
        'logFile'        => '@logs/application.trace.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
        'logVars'        => [],
        'levels'         => ['trace'],
        'enabled'        => YII_DEBUG
    ],
    'query' => [
        'class'          => 'yii\log\FileTarget',
        'categories'     => ['yii\db\Command*'],
        'logFile'        => '@logs/query.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
        'logVars'        => [],
        'levels'         => ['info'],
        'enabled'        => YII_DEBUG
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'categories'     => ['billing'],
        'logFile'        => '@logs/billing.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'categories'     => ['billing'],
        'logFile'        => '@logs/billing.error.' . date('Y-m-d') . '.log',
        'levels'         => ['error'],
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'categories'     => ['mailer'],
        'logFile'        => '@logs/mailer.' . date('Y-m-d') . '.log',
        'logVars'        => [],
        'exportInterval' => 1,
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'levels'         => ['info'],
        'categories'     => ['pipeline'],
        'logFile'        => '@logs/pipeline/pipeline.info.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'levels'         => ['error', 'warning'],
        'categories'     => ['pipeline'],
        'logFile'        => '@logs/pipeline/pipeline.error.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'levels'         => ['info'],
        'categories'     => ['trello'],
        'logFile'        => '@logs/trello/trello.info.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'levels'         => ['error', 'warning'],
        'categories'     => ['trello'],
        'logFile'        => '@logs/trello/trello.error.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'levels'         => ['info'],
        'categories'     => ['push'],
        'logFile'        => '@logs/push/push.info.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'levels'         => ['error', 'warning'],
        'categories'     => ['push'],
        'logFile'        => '@logs/push/push.error.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
    ],
    [
        'class'          => 'yii\log\FileTarget',
        'categories'     => ['push_server'],
        'logFile'        => '@logs/push/push_server.' . date('Y-m-d') . '.log',
        'exportInterval' => 1,
        'logVars'        => [],
    ],
];
一些AdvDbTarget方法:

/**
 * Stores log messages to DB.
 */
public function export()
{
    $this->enableLogging(false); //Here I try to avoid logging of execute info
    $tableName = $this->db->quoteTableName($this->logTable);
    $sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[prefix]], [[message]], [[info]], [[url]], [[application]], [[application_id]], [[user_id]], [[device_id]])
            VALUES (:level, :category, :log_time, :prefix, :message, :info, :url, :application, :application_id, :user_id, :device_id)";
    $command = $this->db->createCommand($sql);
    foreach ($this->messages as $message) {
        list($text, $level, $category, $timestamp) = $message;
        if (!is_string($text)) {
            if ($text instanceof \Throwable || $text instanceof \Exception) {
                $text = (string) $text;
            } else {
                $text = VarDumper::export($text);
            }
        }
        $url = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:'';
        $application    = Yii::$app->id;
        $application_id = $this->getApplicationId();
        $user_id        = $this->getUserId();
        $device_id      = $this->getDeviceId();
        $info           = $this->getContextMessage();
        $prefix         = $this->getMessagePrefix($message);
        $command->bindValues([
            ':level'          => $level,
            ':category'       => $category,
            ':log_time'       => $timestamp,
            ':prefix'         => $prefix,
            ':message'        => $text,
            ':info'           => $info,
            ':url'            => $url,
            ':application'    => $application,
            ':application_id' => $application_id,
            ':user_id'        => $user_id,
            ':device_id'      => $device_id,
        ])->execute();
    }
    $this->enableLogging(true);
}

/**
 * The method makes commands just like 
 * Yii::$app->log->targets['query']->enabled = false/true
 *
 * @param boolean $enable
 */
protected function enableLogging($enable)
{
    if (!empty($this->disableTargets)) {
        foreach ($this->disableTargets as $target) {
            if (isset(Yii::$app->log->targets[$target])) {
                $targetObject = Yii::$app->log->targets[$target];
                if (!$enable) {
                    $this->_diasbledTargets[$target] = $targetObject->enabled;
                    $targetObject->enabled = $enable;
                } else {
                    $targetObject->enabled = $this->_diasbledTargets[$target];
                }
            }
        }
    }
}
AdvDbTarget扩展了DbTarget。
名为
'query'
的目标是记录来自AdvDbTarget的
yii\db\Command
消息的目标。使用
启用日志记录方法的我的代码无法正常工作。

解决方案是通过自定义命令类扩展
yii\db\Command
,重载
执行方法而不进行跟踪和分析,并像这样配置连接:
$this->db->commandClass=Command::className()