Skip to content

Commit

Permalink
added option to disable query logging in DB command
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed May 7, 2017
1 parent 18bc835 commit 65ea226
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
36 changes: 20 additions & 16 deletions framework/db/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -829,30 +829,32 @@ public function execute()
{
$sql = $this->getSql();

$rawSql = $this->getRawSql();

Yii::info($rawSql, __METHOD__);
$log = $this->db->enableQueryLog;
if ($log) {
$rawSql = $this->getRawSql();
Yii::info($rawSql, __METHOD__);
$token = $rawSql;
}

if ($sql == '') {
return 0;
}

$this->prepare(false);

$token = $rawSql;
try {
Yii::beginProfile($token, __METHOD__);
$log && Yii::beginProfile($token, __METHOD__);

$this->pdoStatement->execute();
$n = $this->pdoStatement->rowCount();

Yii::endProfile($token, __METHOD__);
$log && Yii::endProfile($token, __METHOD__);

$this->refreshTableSchema();

return $n;
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
$log && Yii::endProfile($token, __METHOD__);
throw $this->db->getSchema()->convertException($e, $rawSql);
}
}
Expand All @@ -868,9 +870,12 @@ public function execute()
*/
protected function queryInternal($method, $fetchMode = null)
{
$rawSql = $this->getRawSql();

Yii::info($rawSql, 'yii\db\Command::query');
$log = $this->db->enableQueryLog;
if ($log) {
$rawSql = $this->getRawSql();
Yii::info($rawSql, 'yii\db\Command::query');
$token = $rawSql;
}

if ($method !== '') {
$info = $this->db->getQueryCacheInfo($this->queryCacheDuration, $this->queryCacheDependency);
Expand All @@ -883,7 +888,7 @@ protected function queryInternal($method, $fetchMode = null)
$fetchMode,
$this->db->dsn,
$this->db->username,
$rawSql,
isset($rawSql) ? $rawSql : $this->getRawSql(),
];
$result = $cache->get($cacheKey);
if (is_array($result) && isset($result[0])) {
Expand All @@ -895,9 +900,8 @@ protected function queryInternal($method, $fetchMode = null)

$this->prepare(true);

$token = $rawSql;
try {
Yii::beginProfile($token, 'yii\db\Command::query');
$log && Yii::beginProfile($token, 'yii\db\Command::query');

$this->pdoStatement->execute();

Expand All @@ -911,10 +915,10 @@ protected function queryInternal($method, $fetchMode = null)
$this->pdoStatement->closeCursor();
}

Yii::endProfile($token, 'yii\db\Command::query');
$log && Yii::endProfile($token, 'yii\db\Command::query');
} catch (\Exception $e) {
Yii::endProfile($token, 'yii\db\Command::query');
throw $this->db->getSchema()->convertException($e, $rawSql);
$log && Yii::endProfile($token, 'yii\db\Command::query');
throw $this->db->getSchema()->convertException($e, isset($rawSql) ? $rawSql : $this->getRawSql());
}

if (isset($cache, $cacheKey, $info)) {
Expand Down
7 changes: 7 additions & 0 deletions framework/db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ class Connection extends Component
* @see masters
*/
public $shuffleMasters = true;
/**
* @var bool whether to enable logging and profiling of database queries. Defaults to true.
* You may want to disable this option in a production environment to gain performance
* if you do not need the information being logged.
* @since 2.0.12
*/
public $enableQueryLog = true;

/**
* @var Transaction the currently active transaction
Expand Down
36 changes: 36 additions & 0 deletions tests/framework/db/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,40 @@ public function testNestedTransaction()
$this->assertNotNull($db->transaction);
});
}

public function testEnableQueryLog()
{
$connection = $this->getConnection();
if ($connection->getTableSchema('qlog1', true) !== null) {
$connection->createCommand()->dropTable('qlog1')->execute();
}
if ($connection->getTableSchema('qlog2', true) !== null) {
$connection->createCommand()->dropTable('qlog2')->execute();
}

// enabled
$connection->enableQueryLog = true;

\Yii::getLogger()->messages = [];
$connection->createCommand()->createTable('qlog1', ['id' => 'pk'])->execute();
$this->assertCount(3, \Yii::getLogger()->messages);
$this->assertNotNull($connection->getTableSchema('qlog1', true));

\Yii::getLogger()->messages = [];
$connection->createCommand('SELECT * FROM qlog1')->queryAll();
$this->assertCount(3, \Yii::getLogger()->messages);

// disabled
$connection->enableQueryLog = false;

\Yii::getLogger()->messages = [];
$connection->createCommand()->createTable('qlog2', ['id' => 'pk'])->execute();
$this->assertNotNull($connection->getTableSchema('qlog2', true));
$this->assertCount(0, \Yii::getLogger()->messages);
$connection->createCommand('SELECT * FROM qlog2')->queryAll();
$this->assertCount(0, \Yii::getLogger()->messages);



}
}

0 comments on commit 65ea226

Please sign in to comment.