class Log (View source)

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.

Properties

protected array $queryLog

Cache of logged queries. This will only be used if the query logger is enabled.

protected string $connectionKey

The connection key for which this object is logging.

Methods

__construct($key = 'default')

Constructor.

start($logging_key)

Begin logging queries to the specified connection and logging key.

An
get($logging_key)

Retrieve the query log for the specified logging key so far.

clear($logging_key)

Empty the query log for the specified logging key.

end($logging_key)

Stop logging for the specified logging key.

log(StatementInterface $statement, array $args, float $time, float $start = NULL)

Log a query to all active logging keys.

This
findCaller()

Determine the routine that called this query.

static array
removeDatabaseEntries(array $backtrace, string $driver_namespace)

Removes database related calls from a backtrace array.

array[]
getDebugBacktrace()

Gets the debug backtrace.

Details

__construct($key = 'default')

Constructor.

Parameters

$key

The database connection key for which to enable logging.

start($logging_key)

Begin logging queries to the specified connection and logging key.

If the specified logging key is already running this method does nothing.

Parameters

$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.

An get($logging_key)

Retrieve the query log for the specified logging key so far.

Parameters

$logging_key

The logging key to fetch.

Return Value

An

indexed array of all query records for this logging key.

clear($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.

Parameters

$logging_key

The logging key to empty.

end($logging_key)

Stop logging for the specified logging key.

Parameters

$logging_key

The logging key to stop.

log(StatementInterface $statement, array $args, float $time, float $start = NULL)

Log a query to all active logging keys.

Parameters

StatementInterface $statement

The prepared statement object to log.

array $args

The arguments passed to the statement object.

float $time

The time the query took to execute as a float (in seconds with microsecond precision).

float $start

The time the query started as a float (in seconds since the Unix epoch with microsecond precision).

This findCaller()

Determine the routine that called this query.

Traversing the call stack from the very first call made during the request, we define "the routine that called this query" as the last entry in the call stack that is not any method called from the namespace of the database driver, is not inside the Drupal\Core\Database namespace and does have a file (which excludes call_user_func_array(), anonymous functions and similar). That makes the climbing logic very simple, and handles the variable stack depth caused by the query builders.

See the @link http://php.net/debug_backtrace debug_backtrace() @endlink function.

Return Value

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.

static array removeDatabaseEntries(array $backtrace, string $driver_namespace)

Removes database related calls from a backtrace array.

Parameters

array $backtrace

A standard PHP backtrace. Passed by reference.

string $driver_namespace

The PHP namespace of the database driver.

Return Value

array

The cleaned backtrace array.

protected array[] getDebugBacktrace()

Gets the debug backtrace.

Wraps the debug_backtrace function to allow mocking results in PHPUnit tests.

Return Value

array[]

The debug backtrace.