RequestQueryCounter
extends AbstractLogger
in package
PSR-3 logger that counts queries and accumulates their durations for the current process / request.
This is installed on the primary CakePHP database connection so per-request capacity signals (query_count, db_total_ms) are emitted regardless of whether PERF_DB_QUERY_LOG_ENABLED is on. When the connection already had a Cake QueryLogger configured, RequestQueryCounter delegates to it so the existing queries log channel is preserved.
A single shared instance is exposed via RequestQueryCounter::instance() so the performance middleware can read totals at request end without needing DI plumbing.
Table of Contents
Properties
- $count : int
- $currentRequestContext : array<string, string|bool>
- $inner : LoggerInterface|null
- $instance : self|null
- $requestContext : array<string, string|bool>
- $suppressDelegation : bool
- $totalMs : float
Methods
- __construct() : mixed
- beginRequest() : void
- Starts a new HTTP request logging scope for subsequent query log entries.
- clearRequest() : void
- Clears request-specific query log context for long-lived workers.
- count() : int
- currentRequestContext() : array<string, string|bool>
- instance() : self
- Returns (and lazily creates) the process-wide instance. Tests should call {@see reset()} between cases.
- log() : void
- reset() : void
- Resets per-request counters. Safe to call between requests under long-lived workers (workerman, swoole) or in tests.
- setInstance() : void
- Replace the shared instance. Used at bootstrap when the connection's existing logger needs to be wrapped.
- totalMs() : float
- formatQueryLogMessage() : string
- Adds compact request metadata to the rendered query log line.
Properties
$count
private
int
$count
= 0
$currentRequestContext
private
static array<string, string|bool>
$currentRequestContext
= []
$inner
private
LoggerInterface|null
$inner
$instance
private
static self|null
$instance
= null
$requestContext
private
array<string, string|bool>
$requestContext
= []
$suppressDelegation
private
bool
$suppressDelegation
= false
$totalMs
private
float
$totalMs
= 0.0
Methods
__construct()
public
__construct([LoggerInterface|null $inner = null ]) : mixed
Parameters
- $inner : LoggerInterface|null = null
-
Optional inner logger to forward to (e.g. Cake QueryLogger).
beginRequest()
Starts a new HTTP request logging scope for subsequent query log entries.
public
beginRequest(string $requestId, string $method, string $host, string $path, string $target, string $turboFrame, bool $isAjax[, bool $suppressDelegation = false ]) : void
Parameters
- $requestId : string
-
Short correlation ID for this HTTP request.
- $method : string
-
HTTP method.
- $host : string
-
Request host without credentials.
- $path : string
-
Request path without query string.
- $target : string
-
Request path and query string, for diagnostics.
- $turboFrame : string
-
Turbo Frame header value, or empty string when absent.
- $isAjax : bool
-
Whether the request is an XMLHttpRequest.
- $suppressDelegation : bool = false
-
Whether query logs should remain local to the counter.
clearRequest()
Clears request-specific query log context for long-lived workers.
public
clearRequest() : void
count()
public
count() : int
Return values
int —Number of queries observed during the current request.
currentRequestContext()
public
static currentRequestContext() : array<string, string|bool>
Return values
array<string, string|bool> —Current HTTP request context for log formatters.
instance()
Returns (and lazily creates) the process-wide instance. Tests should call {@see reset()} between cases.
public
static instance() : self
Return values
selflog()
public
log(mixed $level, Stringable|string $message[, array<string, mixed> $context = [] ]) : void
Parameters
- $level : mixed
-
PSR log level
- $message : Stringable|string
-
Log message
- $context : array<string, mixed> = []
-
Log context (may include
query)
reset()
Resets per-request counters. Safe to call between requests under long-lived workers (workerman, swoole) or in tests.
public
reset() : void
setInstance()
Replace the shared instance. Used at bootstrap when the connection's existing logger needs to be wrapped.
public
static setInstance(self $instance) : void
Parameters
- $instance : self
totalMs()
public
totalMs() : float
Return values
float —Total query duration accumulated this request, in milliseconds.
formatQueryLogMessage()
Adds compact request metadata to the rendered query log line.
private
formatQueryLogMessage(Stringable|string $message) : string
Parameters
- $message : Stringable|string
-
Original query log message.
Return values
string —Query log message with request correlation details.