Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Commits
dc1c2946
Commit
dc1c2946
authored
16 years ago
by
Dries Buytaert
Browse files
Options
Downloads
Patches
Plain Diff
- Forgot to commit log.inc.
parent
8f533b9e
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!7452
Issue #1797438. HTML5 validation is preventing form submit and not fully...
,
!789
Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
includes/database/log.inc
+159
-0
159 additions, 0 deletions
includes/database/log.inc
with
159 additions
and
0 deletions
includes/database/log.inc
0 → 100644
+
159
−
0
View file @
dc1c2946
<?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
* database target. If we logged the queries in each connection object we
* 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
{
/**
* Cache of logged queries. This will only be used if the query logger is enabled.
*
* 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
* The identification key for this log request. By specifying different
* 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.
*
* This method does not stop logging, it simply clears the log. To stop
* 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.
*/
public
function
log
(
DatabaseStatement
$statement
,
$args
,
$time
)
{
foreach
(
array_keys
(
$this
->
queryLog
)
as
$key
)
{
$this
->
queryLog
[
$key
][]
=
array
(
'query'
=>
$statement
->
queryString
,
'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
* the call stack that is not inside includes/database. That makes the
* 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
* debug_backtrace(). However, it flattens the trace entry and the trace
* 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
)
{
if
(
strpos
(
$stack
[
$i
][
'file'
],
'includes/database'
)
===
FALSE
)
{
return
array
(
'file'
=>
$stack
[
$i
][
'file'
],
'line'
=>
$stack
[
$i
][
'line'
],
'function'
=>
$stack
[
$i
+
1
][
'function'
],
'args'
=>
$stack
[
$i
+
1
][
'args'
],
);
return
$stack
[
$i
];
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment