123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- class DatabaseLog {
-
- protected $queryLog = array();
-
- protected $connectionKey = 'default';
-
- public function __construct($key = 'default') {
- $this->connectionKey = $key;
- }
-
- public function start($logging_key) {
- if (empty($this->queryLog[$logging_key])) {
- $this->clear($logging_key);
- }
- }
-
- public function get($logging_key) {
- return $this->queryLog[$logging_key];
- }
-
- public function clear($logging_key) {
- $this->queryLog[$logging_key] = array();
- }
-
- public function end($logging_key) {
- unset($this->queryLog[$logging_key]);
- }
-
- public function log(DatabaseStatementInterface $statement, $args, $time) {
- foreach (array_keys($this->queryLog) as $key) {
- $this->queryLog[$key][] = array(
- 'query' => $statement->getQueryString(),
- 'args' => $args,
- 'target' => $statement->dbh->getTarget(),
- 'caller' => $this->findCaller(),
- 'time' => $time,
- );
- }
- }
-
- public function findCaller() {
- $stack = debug_backtrace();
- $stack_count = count($stack);
- for ($i = 0; $i < $stack_count; ++$i) {
- if (!empty($stack[$i]['file']) && strpos($stack[$i]['file'], 'includes' . DIRECTORY_SEPARATOR . 'database') === FALSE) {
- $stack[$i] += array('args' => array());
- return array(
- 'file' => $stack[$i]['file'],
- 'line' => $stack[$i]['line'],
- 'function' => $stack[$i + 1]['function'],
- 'class' => isset($stack[$i + 1]['class']) ? $stack[$i + 1]['class'] : NULL,
- 'type' => isset($stack[$i + 1]['type']) ? $stack[$i + 1]['type'] : NULL,
- 'args' => $stack[$i + 1]['args'],
- );
- }
- }
- }
- }
|