CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 5.4 Chiffon API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 5.4
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
    • Core
    • Database
      • Driver
      • Exception
      • Expression
      • Log
      • Query
      • Retry
      • Schema
      • Statement
      • Type
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

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.

Namespace: Cake\Database\Log

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()) avoids property_exists (which would also accept static properties like redactor and create dynamic instance properties on assignment) and reflection-based filtering.

Property Summary

  • $driver protected
    Cake\Database\Driver|null

    Driver executing the query

  • $error protected
    Exception|null

    The exception that was thrown by the execution of this query

  • $numRows protected
    int

    Number of rows affected or returned by the query execution

  • $params protected
    array

    Associative array with the params bound to the query string

  • $query protected
    string

    Query string that was executed

  • $redactor protected static
    Closure|null

    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.

  • $took protected
    float

    Number 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
string

getConnectionName() ¶ public

getConnectionName(): string

Get the connection name from the driver config.

Returns
string

getContext() ¶ 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
string

jsonSerialize() ¶ 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
RuntimeException
If 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
void

setRedactor() ¶ 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
void

Property Detail

$driver ¶ protected

Driver executing the query

Type
Cake\Database\Driver|null

$error ¶ protected

The exception that was thrown by the execution of this query

Type
Exception|null

$numRows ¶ protected

Number of rows affected or returned by the query execution

Type
int

$params ¶ protected

Associative array with the params bound to the query string

Type
array

$query ¶ protected

Query string that was executed

Type
string

$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

$took ¶ protected

Number of milliseconds this query took to complete

Type
float
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs