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

时间:2017-05-14 22:00:31

标签: yii2

我创建了自己的日志目标,如DbTarget,在记录时记录DB中的某些特定信息,但每个执行查询都被其他目标记录。如何禁用它?

目标配置:

public void dls(int limit) {
    Set<Integer> visited = new HashSet<>();
    int start = 0;
    stack.push(start);
    display(start);
    int depth = 1;
    while (!stack.isEmpty()) {
        int current = stack.peek();
        visited.add(current);
        int next = -1;
        for (int adj = 0; adj < noOfNodes; adj++) {
            if (neighbourMatrix[current][adj] == 1 && !visited.contains(adj)) {
                next = adj;
            }
        }
        if (depth <= limit) {
            if (next == -1) {
                stack.pop();
                depth--;
            } else {
                stack.push(next);
                display(next);
                depth++;
            }
        } else {
            stack.pop();
            depth--;
        }
    }
}

一些AdvDbTarget方法:

$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扩展了DbTarget。 名为/** * 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记录'query'消息的目标。使用yii\db\Command方法的代码无效。

1 个答案:

答案 0 :(得分:0)

解决方案是通过自定义命令类扩展yii\db\Command,重载execute方法而不跟踪&amp;分析和配置连接,如下所示:$this->db->commandClass = Command::className();