log.inc 4.45 KB
Newer Older
Dries's avatar
Dries committed
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// $Id$

/**
 * @file
 * Logging classes for the database layer.
 */

/**
 * Database query logger.
 *
 * We log queries in a separate object rather than in the connection object
 * because we want to be able to see all queries sent to a given database, not
14
 * database target. If we logged the queries in each connection object we
Dries's avatar
Dries committed
15
16
17
18
19
20
21
22
 * would not be able to track what queries went to which target.
 *
 * Every connection has one and only one logging object on it for all targets
 * and logging keys.
 */
class DatabaseLog {

  /**
23
   * Cache of logged queries. This will only be used if the query logger is enabled.
Dries's avatar
Dries committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
   *
   * The structure for the logging array is as follows:
   *
   * array(
   *   $logging_key = array(
   *     array(query => '', args => array(), caller => '', target => '', time => 0),
   *     array(query => '', args => array(), caller => '', target => '', time => 0),
   *   ),
   * );
   *
   * @var array
   */
  protected $queryLog = array();

  /**
   * The connection key for which this object is logging.
   *
   * @var string
   */
  protected $connectionKey = 'default';

  /**
   * Constructor.
   *
   * @param $key
   *   The database connection key for which to enable logging.
   */
  public function __construct($key = 'default') {
    $this->connectionKey = $key;
  }

  /**
   * Begin logging queries to the specified connection and logging key.
   *
   * If the specified logging key is already running this method does nothing.
   *
   * @param $logging_key
61
   *   The identification key for this log request. By specifying different
Dries's avatar
Dries committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
   *   logging keys we are able to start and stop multiple logging runs
   *   simultaneously without them colliding.
   */
  public function start($logging_key) {
    if (empty($this->queryLog[$logging_key])) {
      $this->clear($logging_key);
    }
  }

  /**
   * Retrieve the query log for the specified logging key so far.
   *
   * @param $logging_key
   *   The logging key to fetch.
   * @return
   *   An indexed array of all query records for this logging key.
   */
  public function get($logging_key) {
    return $this->queryLog[$logging_key];
  }

  /**
   * Empty the query log for the specified logging key.
   *
86
   * This method does not stop logging, it simply clears the log. To stop
Dries's avatar
Dries committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
   * logging, use the end() method.
   *
   * @param $logging_key
   *   The logging key to empty.
   */
  public function clear($logging_key) {
    $this->queryLog[$logging_key] = array();
  }

  /**
   * Stop logging for the specified logging key.
   *
   * @param $logging_key
   *   The logging key to stop.
   */
  public function end($logging_key) {
    unset($this->queryLog[$logging_key]);
  }

  /**
   * Log a query to all active logging keys.
   *
   * @param $statement
   *   The prepared statement object to log.
   * @param $args
   *   The arguments passed to the statement object.
   * @param $time
   *   The time in milliseconds the query took to execute.
   */
116
  public function log(DatabaseStatementInterface $statement, $args, $time) {
Dries's avatar
Dries committed
117
118
    foreach (array_keys($this->queryLog) as $key) {
      $this->queryLog[$key][] = array(
119
        'query' => $statement->getQueryString(),
Dries's avatar
Dries committed
120
121
122
123
124
125
126
127
128
129
130
131
        'args' => $args,
        'target' => $statement->dbh->getTarget(),
        'caller' => $this->findCaller(),
        'time' => $time,
      );
    }
  }

  /**
   * Determine the routine that called this query.
   *
   * We define "the routine that called this query" as the first entry in
132
   * the call stack that is not inside includes/database. That makes the
Dries's avatar
Dries committed
133
134
135
136
137
138
   * climbing logic very simple, and handles the variable stack depth caused
   * by the query builders.
   *
   * @link http://www.php.net/debug_backtrace
   * @return
   *   This method returns a stack trace entry similar to that generated by
139
   *   debug_backtrace(). However, it flattens the trace entry and the trace
Dries's avatar
Dries committed
140
141
142
143
144
145
146
147
   *   entry before it so that we get the function and args of the function that
   *   called into the database system, not the function and args of the
   *   database call itself.
   */
  public function findCaller() {
    $stack = debug_backtrace();
    $stack_count = count($stack);
    for ($i = 0; $i < $stack_count; ++$i) {
148
      if (strpos($stack[$i]['file'], 'includes' . DIRECTORY_SEPARATOR . 'database') === FALSE) {
Dries's avatar
Dries committed
149
150
151
152
153
154
155
156
157
158
        return array(
          'file' => $stack[$i]['file'],
          'line' => $stack[$i]['line'],
          'function' => $stack[$i + 1]['function'],
          'args' => $stack[$i + 1]['args'],
        );
        return $stack[$i];
      }
    }
  }
159
}