Class LoggedQuery
Contains a query string, the params used to executed it, time taken to do it and the number of rows found or affected by its execution.
Applications can register a {@see LoggedQuery::setRedactor()} closure to scrub sensitive bound values from every public exposure path.
Constants
-
list<string>CONTEXT_KEYS ¶['driver', 'query', 'took', 'params', 'numRows', 'error']Instance-property allowlist for {@see setContext()}. Hardcoded so the hot path (called per query from
Driver::log()) avoidsproperty_exists(which would also accept static properties likeredactorand create dynamic instance properties on assignment) and reflection-based filtering.
Property Summary
-
$driver protected
Cake\Database\Driver|nullDriver executing the query
-
$error protected
Exception|nullThe exception that was thrown by the execution of this query
-
$numRows protected
intNumber of rows affected or returned by the query execution
-
$params protected
arrayAssociative array with the params bound to the query string
-
$query protected
stringQuery string that was executed
-
$redactor protected static
Closure|nullOptional redactor invoked before the stored query string or bound parameters are exposed via {@see __toString()}, {@see getContext()}, or {@see jsonSerialize()}. Receives
(string $query, array $params)and must return a 2-element list[string $query, array $params]with sensitive substrings replaced. -
$took protected
floatNumber of milliseconds this query took to complete
Method Summary
-
__toString() public
Returns the string representation of this logged query
-
getConnectionName() public
Get the connection name from the driver config.
-
getContext() public
Get the logging context data for a query.
-
interpolate() protected
Helper function used to replace query placeholders by the real params used to execute the query
-
jsonSerialize() public
Returns data that will be serialized as JSON
-
redacted() protected
Apply the configured redactor (if any) to the stored query+params and return the sanitized tuple.
-
setContext() public
Set logging context for this query.
-
setRedactor() public static
Register a global redactor applied to every LoggedQuery before its query string or params are exposed for logging.
Method Detail
__toString() ¶ public
__toString(): string
Returns the string representation of this logged query
Returns
stringgetConnectionName() ¶ public
getConnectionName(): string
Get the connection name from the driver config.
Returns
stringgetContext() ¶ public
getContext(): array<string, mixed>
Get the logging context data for a query.
Returns
array<string, mixed>interpolate() ¶ protected
interpolate(): string
Helper function used to replace query placeholders by the real params used to execute the query
Returns
stringjsonSerialize() ¶ public
jsonSerialize(): array<string, mixed>
Returns data that will be serialized as JSON
Returns
array<string, mixed>redacted() ¶ protected
redacted(): array{0: string, 1: array}
Apply the configured redactor (if any) to the stored query+params and return the sanitized tuple.
A redactor that throws lets the exception propagate; a redactor
that returns a value not matching [string, array] raises a
RuntimeException. Both surface a broken redactor instead of
silently falling back to the raw values it was meant to scrub.
Returns
array{0: string, 1: array}[sanitized query, sanitized params]
Throws
RuntimeExceptionIf the redactor returns a malformed value.
setContext() ¶ public
setContext(array<string, mixed> $context): void
Set logging context for this query.
Parameters
-
array<string, mixed>$context Context data.
Returns
voidsetRedactor() ¶ public static
setRedactor(Closure|null $redactor): void
Register a global redactor applied to every LoggedQuery before its query string or params are exposed for logging.
The redactor receives the raw (string $query, array $params) and
must return a 2-element list [string $query, array $params] with
the sensitive substrings replaced. Exceptions thrown by the
redactor propagate to the caller, and returning a malformed value
raises a RuntimeException — both surface a broken redactor
loudly rather than silently leaking the secrets it was supposed
to scrub. Register a redactor you trust.
Pass null to clear a previously-registered redactor.
Parameters
-
Closure|null$redactor fn(string, array): array{0: string, 1: array}
Returns
voidProperty Detail
$redactor ¶ protected static
Optional redactor invoked before the stored query string or bound
parameters are exposed via {@see __toString()}, {@see getContext()},
or {@see jsonSerialize()}. Receives (string $query, array $params)
and must return a 2-element list [string $query, array $params]
with sensitive substrings replaced.
Used to keep secrets bound as parameters (cryptographic keys, passwords, tokens) out of file logs, structured loggers, and remote breadcrumb sinks. Set once during application bootstrap; applies to every LoggedQuery instance from then on.
Type
Closure|null