Log

Reference for the Log module: write typed log messages gated by a configured level, record named numeric counters aggregated into per-minute timeslots, time callbacks, and buffer counter increments in shared memory. Every class and function, with its full signature, parameters and return type.

This page is a pure reference. For how the framework fits together, see the Guide.

Classes
  • Log - static functions for writing typed log messages, gated by the configured log level.
  • LogConfig - registers callbacks invoked when an alert is logged.
  • LogCounter - records named numeric values and increments, buffered or written straight to the database.
  • LogCounterBuffer - an APCu-backed write buffer that folds counter increments into the database once per minute.
  • LogCounterCountByDayData - a counter total for one calendar day.
  • LogCounterCountByHourData - a counter total for one hour of the day.
  • LogCounterCountByMonthData - a counter total for one calendar month.
  • LogCounterData - one aggregated counter row for a module, identifier, argument and timeslot.
  • LogCounterDatabase - the database layer for counters: upserts, time-bucketing and aggregate reads.
  • LogDatabase - the database layer for log messages.
  • LogTimer - times a callback and records the elapsed seconds as a counter value.
  • LogTypeEnum - the numeric log level constants, from alert down to debug.

Log

Static functions for writing log messages. Each level is only written when the configured LOG_LEVEL is at least that level; every message also increments a counter for its level. Writes go to the database, with a fallback to the error file and then the registered alert callbacks.

Alert

Log::Alert(string $moduleName, string $message, mixed $details = []): void

Log an alert: write it to the database and the error file, then run every registered alert callback. Also increments the ALERT counter.

NameTypeDescription
$moduleNamestringThe module the alert relates to.
$messagestringThe alert message.
$detailsmixedExtra detail, encoded into the log entry. Defaults to an empty array.

Returns void

Debug

Log::Debug(string $moduleName, string $message, mixed $details = []): void

Log a debug message when the log level allows it. Also increments the DEBUG counter.

NameTypeDescription
$moduleNamestringThe module the message relates to.
$messagestringThe debug message.
$detailsmixedExtra detail, encoded into the log entry. Defaults to an empty array.

Returns void

encodeDetails

Log::encodeDetails(array $details): string

Encode an associative details array to text, tolerating individual entries that cannot be JSON encoded (a bad value becomes an error marker; a bad key is dropped).

NameTypeDescription
$detailsarrayThe details to encode.

Returns string The encoded details text.

Error

Log::Error(string $moduleName, string $message, array $details = []): void

Log an error when the log level allows it, falling back to the error file and then the alert callbacks if the database write fails. Also increments the ERROR counter.

NameTypeDescription
$moduleNamestringThe module the error relates to.
$messagestringThe error message.
$detailsarrayExtra detail, encoded into the log entry. Defaults to an empty array.

Returns void

Exception

Log::Exception(string $moduleName, string $message, Throwable $ex, array $details = []): void

Log a caught throwable, recording its type, message, code, file, line and stack trace alongside the given details. Also increments the EXCEPTION counter.

NameTypeDescription
$moduleNamestringThe module the exception relates to.
$messagestringA message describing the context.
$exThrowableThe caught throwable to record.
$detailsarrayExtra detail, encoded into the log entry. Defaults to an empty array.

Returns void

Info

Log::Info(string $moduleName, string $message, array $details = []): void

Log an informational message when the log level allows it. Also increments the INFO counter.

NameTypeDescription
$moduleNamestringThe module the message relates to.
$messagestringThe informational message.
$detailsarrayExtra detail, encoded into the log entry. Defaults to an empty array.

Returns void

LogConfig

The configuration surface for the Log module: register callbacks that run whenever an alert is logged. The registered callbacks are also used as a last-resort fallback when a log write and the error file both fail.

Properties

PropertyTypeDescription
LogConfig::$onAlertsarrayThe registered alert callbacks, in registration order.
Functions

OnAlert

LogConfig::OnAlert(callable $callback): void

Register a callback to run whenever an alert is logged. It is called with the module name, message and details.

NameTypeDescription
$callbackcallableThe callback to register.

Returns void

LogCounter

Records named numeric values, aggregated over time. Value and Increment are buffered: increments accumulate in shared memory and are folded into the database once per completed one-minute timeslot. For a counter that must never lose a value, the Exact variants write straight to the database. When APCu is not loaded the buffered calls fall back to the direct path.

Increment

LogCounter::Increment(string $moduleName, string $counterIdentifier, string|null $optionalArgumentString = null): void

Increment a counter by one for the current timeslot, buffered. A failure is logged rather than thrown, so counting never breaks the caller.

NameTypeDescription
$moduleNamestringThe module the counter belongs to.
$counterIdentifierstringThe counter name; dots are allowed.
$optionalArgumentStringstring|nullAn optional argument that further distinguishes the counter. Defaults to null.

Returns void

IncrementExact

LogCounter::IncrementExact(string $moduleName, string $counterIdentifier, string|null $optionalArgumentString = null): void

Like Increment but exact and durable: never buffered, written straight to the database. A failure is swallowed so the counter never breaks the caller.

NameTypeDescription
$moduleNamestringThe module the counter belongs to.
$counterIdentifierstringThe counter name; dots are allowed.
$optionalArgumentStringstring|nullAn optional argument that further distinguishes the counter. Defaults to null.

Returns void

Value

LogCounter::Value(string $moduleName, string $counterIdentifier, string|null $optionalArgumentString, float $value): void

Aggregate a value into the current timeslot, buffered. The timeslot accumulates the total, count, minimum and maximum.

NameTypeDescription
$moduleNamestringThe module the counter belongs to.
$counterIdentifierstringThe counter name; dots are allowed.
$optionalArgumentStringstring|nullAn optional argument that further distinguishes the counter.
$valuefloatThe value to aggregate.

Returns void

ValueExact

LogCounter::ValueExact(string $moduleName, string $counterIdentifier, string|null $optionalArgumentString, float $value): void

Like Value but written straight to the database: never buffered, never lost. Slower under high concurrency; use for durable counters.

NameTypeDescription
$moduleNamestringThe module the counter belongs to.
$counterIdentifierstringThe counter name; dots are allowed.
$optionalArgumentStringstring|nullAn optional argument that further distinguishes the counter.
$valuefloatThe value to aggregate.

Returns void

LogCounterBuffer

An APCu-backed write buffer in front of the counter table. Increments accumulate in shared memory bucketed by one-minute timeslot; once a minute completes its slot is frozen and folded into the database in a single batched write. Falls back to a direct write wherever APCu is not loaded.

Add

LogCounterBuffer::Add(string $moduleName, string $counterIdentifier, string|null $argument, float $value): void

Buffer one value into the current timeslot, then opportunistically flush any completed timeslots. Assumes the caller has already validated the identifiers.

NameTypeDescription
$moduleNamestringThe module the counter belongs to.
$counterIdentifierstringThe counter name.
$argumentstring|nullAn optional argument that further distinguishes the counter.
$valuefloatThe value to buffer.

Returns void

Available

LogCounterBuffer::Available(): bool

Whether the APCu extension is loaded and its cache is enabled, so buffering is used rather than direct writes. The result is cached for the request.

Returns bool True when buffering is available.

Flush

LogCounterBuffer::Flush(): void

Force-flush every completed timeslot to the database. A no-op when buffering is not available. Used from the shutdown handler.

Returns void

MaybeFlush

LogCounterBuffer::MaybeFlush(int $currentSlot): void

If the minute has rolled over since the last seen slot, a single winner takes a short-lived lock and flushes every completed timeslot.

NameTypeDescription
$currentSlotintThe current timeslot timestamp.

Returns void

LogCounterCountByDayData

A data object holding a counter total for one calendar day, returned by LogCounterDatabase::GetCountsByDay.

Properties

PropertyTypeDescription
$datestringThe day, as YYYY-MM-DD (readonly).
$countintThe total count for the day (readonly).
Functions

FromJson

LogCounterCountByDayData::FromJson(string $json): LogCounterCountByDayData

Build a LogCounterCountByDayData from its JSON representation.

NameTypeDescription
$jsonstringThe JSON string.

Returns LogCounterCountByDayData The decoded data object.

ToJson

LogCounterCountByDayData::ToJson(): string

Serialise the day total to JSON.

Returns string The JSON representation.

LogCounterCountByHourData

A data object holding a counter total for one hour of the day, returned by LogCounterDatabase::GetCountsByHour.

Properties

PropertyTypeDescription
$hourintThe hour of the day, 0 to 23 (readonly).
$countintThe total count for the hour (readonly).
Functions

FromJson

LogCounterCountByHourData::FromJson(string $json): LogCounterCountByHourData

Build a LogCounterCountByHourData from its JSON representation.

NameTypeDescription
$jsonstringThe JSON string.

Returns LogCounterCountByHourData The decoded data object.

ToJson

LogCounterCountByHourData::ToJson(): string

Serialise the hour total to JSON.

Returns string The JSON representation.

LogCounterCountByMonthData

A data object holding a counter total for one calendar month, returned by LogCounterDatabase::GetCountsByMonth.

Properties

PropertyTypeDescription
$yearintThe year (readonly).
$monthintThe month, 1 to 12 (readonly).
$countintThe total count for the month (readonly).
Functions

FromJson

LogCounterCountByMonthData::FromJson(string $json): LogCounterCountByMonthData

Build a LogCounterCountByMonthData from its JSON representation.

NameTypeDescription
$jsonstringThe JSON string.

Returns LogCounterCountByMonthData The decoded data object.

ToJson

LogCounterCountByMonthData::ToJson(): string

Serialise the month total to JSON.

Returns string The JSON representation.

LogCounterData

A data object for one aggregated counter row: the module, identifier and optional argument, its hash and timeslot, and the accumulated total, minimum, maximum and count.

Properties

PropertyTypeDescription
$idintThe row id (readonly).
$moduleNamestringThe module the counter belongs to (readonly).
$counterIdentifierstringThe counter name (readonly).
$hashstringThe unique hash key for the counter and timeslot (readonly).
$argument?stringThe optional distinguishing argument, or null (readonly).
$timeslotTimestampintThe start timestamp of the timeslot (readonly).
$totalfloatThe sum of values in the timeslot (readonly).
$minimumfloatThe smallest value in the timeslot (readonly).
$maximumfloatThe largest value in the timeslot (readonly).
$countintThe number of values aggregated (readonly).
Functions

FromJson

LogCounterData::FromJson(string $json): LogCounterData

Build a LogCounterData from its JSON representation.

NameTypeDescription
$jsonstringThe JSON string.

Returns LogCounterData The decoded data object.

ToJson

LogCounterData::ToJson(): string

Serialise the counter row to JSON.

Returns string The JSON representation.

LogCounterDatabase

The database layer for counters: upserting values into the current timeslot, time-bucketing and hashing, and aggregate reads grouped by hour, day, month or timeslot. Counters are bucketed into fixed-length timeslots.

Constants

ConstantValueDescription
LogCounterDatabase::TIMESLOT_LENGTH_SECONDS60The length in seconds of each counter timeslot.

BulkFetchCounters

LogCounterDatabase::BulkFetchCounters(int $startTimestamp, ?string $optionalModuleName = null, ?string $optionalCounterIdentifier = null, ?string $optionalArgument = null): array

Fetch counter rows from a start time, optionally filtered by module, identifier and argument.

NameTypeDescription
$startTimestampintThe earliest timeslot timestamp to include.
$optionalModuleName?stringA module to filter by, or null for all. Defaults to null.
$optionalCounterIdentifier?stringA counter identifier to filter by, or null for all. Defaults to null.
$optionalArgument?stringAn argument to filter by, or null for all. Defaults to null.

Returns array A list of LogCounterData rows.

FetchTimeSeriesForCounter

LogCounterDatabase::FetchTimeSeriesForCounter(int $startTimestamp, string $moduleName, string $counterIdentifier): array

Sum a counter total per timeslot from a start time, ready for charting. The result holds a totals map keyed by timeslot timestamp and the summed totalCount.

NameTypeDescription
$startTimestampintThe earliest timeslot timestamp to include.
$moduleNamestringThe module the counter belongs to.
$counterIdentifierstringThe counter name.

Returns array A map with a totals array and a totalCount.

fromRow

LogCounterDatabase::fromRow(object $row): LogCounterData

Build a LogCounterData from a database row.

NameTypeDescription
$rowobjectThe database row.

Returns LogCounterData The mapped data object.

GetCountsByDay

LogCounterDatabase::GetCountsByDay(int $startTimestamp, ?string $optionalModuleName = null, ?string $optionalCounterIdentifier = null, ?string $optionalArgument = null): array

Counter counts grouped by calendar day from a start time, sorted by date.

NameTypeDescription
$startTimestampintThe earliest timeslot timestamp to include.
$optionalModuleName?stringA module to filter by, or null for all. Defaults to null.
$optionalCounterIdentifier?stringA counter identifier to filter by, or null for all. Defaults to null.
$optionalArgument?stringAn argument to filter by, or null for all. Defaults to null.

Returns array A list of LogCounterCountByDayData.

GetCountsByHour

LogCounterDatabase::GetCountsByHour(int $startTimestamp, ?string $optionalModuleName = null, ?string $optionalCounterIdentifier = null, ?string $optionalArgument = null): array

Counter counts grouped by hour of the day from a start time, one entry for each of the 24 hours.

NameTypeDescription
$startTimestampintThe earliest timeslot timestamp to include.
$optionalModuleName?stringA module to filter by, or null for all. Defaults to null.
$optionalCounterIdentifier?stringA counter identifier to filter by, or null for all. Defaults to null.
$optionalArgument?stringAn argument to filter by, or null for all. Defaults to null.

Returns array A list of LogCounterCountByHourData.

GetCountsByMonth

LogCounterDatabase::GetCountsByMonth(int $startTimestamp, ?string $optionalModuleName = null, ?string $optionalCounterIdentifier = null, ?string $optionalArgument = null): array

Counter counts grouped by calendar month from a start time, sorted by year then month.

NameTypeDescription
$startTimestampintThe earliest timeslot timestamp to include.
$optionalModuleName?stringA module to filter by, or null for all. Defaults to null.
$optionalCounterIdentifier?stringA counter identifier to filter by, or null for all. Defaults to null.
$optionalArgument?stringAn argument to filter by, or null for all. Defaults to null.

Returns array A list of LogCounterCountByMonthData.

getCurrentTimeslot

LogCounterDatabase::getCurrentTimeslot(): int

The current time rounded down to the start of its timeslot, as a Unix timestamp.

Returns int The current timeslot timestamp.

GetIdentifiersByModuleName

LogCounterDatabase::GetIdentifiersByModuleName(string $moduleName): array

The distinct counter identifiers recorded for a module, sorted.

NameTypeDescription
$moduleNamestringThe module to read identifiers for.

Returns array A list of counter identifier strings.

Hash

LogCounterDatabase::Hash(string $moduleName, string $counterIdentifier, string|null $optionalArgumentString, int $timeslotTimestamp): string

The unique hash key for a counter, derived from its module, identifier, optional argument and timeslot.

NameTypeDescription
$moduleNamestringThe module the counter belongs to.
$counterIdentifierstringThe counter name.
$optionalArgumentStringstring|nullAn optional argument that further distinguishes the counter.
$timeslotTimestampintThe timeslot timestamp.

Returns string The counter hash.

Value

LogCounterDatabase::Value(string $moduleName, string $counterIdentifier, string|null $optionalArgumentString, float $value): void

Upsert a value into the current timeslot row for a counter, adding to the total and count and updating the minimum and maximum.

NameTypeDescription
$moduleNamestringThe module the counter belongs to.
$counterIdentifierstringThe counter name.
$optionalArgumentStringstring|nullAn optional argument that further distinguishes the counter.
$valuefloatThe value to aggregate.

Returns void

ValueBatch

LogCounterDatabase::ValueBatch(array $rows): void

Upsert many pre-aggregated counter deltas in one statement, each folded additively into its existing row. Used when the buffer flushes a completed timeslot.

NameTypeDescription
$rowsarrayA list of rows, each with module, identifier, argument, slot, total, count, min and max.

Returns void

LogDatabase

The database layer for log messages: inserting a message row for the Log functions, and a per-minute named counter.

Functions

Add

LogDatabase::Add(int $type, string $moduleName, string $message, string $details): int

Insert a log message row, recording its type, module, message, details and the current timestamp, and return its new id.

NameTypeDescription
$typeintThe message type, one of the LogTypeEnum constants.
$moduleNamestringThe module the message relates to.
$messagestringThe message text.
$detailsstringThe encoded details.

Returns int The new row id.

Count

LogDatabase::Count(string $moduleName, string $counterName): void

Increment a named per-minute counter row for a module, ensuring the row exists first.

NameTypeDescription
$moduleNamestringThe module the counter belongs to.
$counterNamestringThe counter name.

Returns void

LogTimer

Times a callback and records the elapsed seconds as a counter value.

Functions

Time

LogTimer::Time(string $moduleName, string $timerName, string|null $optionalArgumentString, callable $code, array ...$argutments): mixed

Run the callback, then record its elapsed run time in seconds as a counter value, even if the callback throws. Returns whatever the callback returns.

NameTypeDescription
$moduleNamestringThe module the timer belongs to.
$timerNamestringThe timer name, used as the counter identifier.
$optionalArgumentStringstring|nullAn optional argument that further distinguishes the counter.
$codecallableThe callback to run and time.
...$argutmentsarrayArguments passed through to the callback.

Returns mixed The value returned by the callback.

LogTypeEnum

The numeric log level constants. A lower number is a higher severity; the configured LOG_LEVEL is the highest number that is written. This class has no functions.

Constants

ConstantValueDescription
LogTypeEnum::TYPE_ALERT1An alert, the highest severity.
LogTypeEnum::TYPE_EXCEPTION2A caught exception.
LogTypeEnum::TYPE_ERROR3An error.
LogTypeEnum::TYPE_INFO4An informational message.
LogTypeEnum::TYPE_DEBUG5A debug message, the lowest severity.