Created
November 27, 2013 08:23
-
-
Save haroonabbasi/7672372 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function afterFilter(){ | |
parent::afterFilter(); | |
// sql logging to chrome console | |
if (class_exists('ConnectionManager') && Configure::read('debug') >= 2) { | |
App::import('Vendor', 'ChromePhp/ChromePhp'); | |
$sources = ConnectionManager::sourceList(); | |
$logs = array(); | |
foreach ($sources as $source){ | |
$db = ConnectionManager::getDataSource($source); | |
$logs[$source] = $db->getLog(); | |
} | |
foreach ($logs as $source => $logInfo){ | |
$text = $logInfo['count'] > 1 ? 'queries' : 'query'; | |
ChromePhp::info('------- SQL: '.sprintf('(%s) %s %s took %s ms', $source, count($logInfo['log']), $text, $logInfo['time']).' -------'); | |
ChromePhp::info('------- REQUEST: '.$this->request->params['controller'].'/'.$this->request->params['action'].' -------'); | |
foreach ($logInfo['log'] as $k => $i){ | |
$i += array('error' => ''); | |
if (!empty($i['params']) && is_array($i['params'])) { | |
$bindParam = $bindType = null; | |
if (preg_match('/.+ :.+/', $i['query'])) { | |
$bindType = true; | |
} | |
foreach ($i['params'] as $bindKey => $bindVal) { | |
if ($bindType === true) { | |
$bindParam .= h($bindKey) ." => " . h($bindVal) . ", "; | |
} else { | |
$bindParam .= h($bindVal) . ", "; | |
} | |
} | |
$i['query'] .= " , params[ " . rtrim($bindParam, ', ') . " ]"; | |
} | |
$error = !empty($i['error']) ? "\nError: ".$i['error']:"\n"; | |
$logStr = $i['query'].$error."\nAffected: ".$i['affected']."\nNum. Rows: ".$i['numRows']."\nTook(ms): ".$i['took']."\n\n"; | |
if(!empty($i['error'])){ | |
ChromePhp::error($logStr); | |
} | |
else if($i['took'] >= 100){ | |
ChromePhp::warn($logStr); | |
} | |
else{ | |
ChromePhp::info($logStr); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment